我需要创建一个对象组成为2个元素的2个数组。
我有数组形式的axios调用:
struct ContentView : View {
@State var totalAmount = ""
let addAmount = 10.0 // or whatever
var calc : Double { (Double(totalAmount) ?? 0.0) + addAmount } // or whatever
var body: some View {
HStack {
TextField("Type a number", text: $totalAmount)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(width:100)
Text(String(self.calc))
}
}
}
如何获取总计数组和月份数组?
答案 0 :(得分:0)
两次使用内置的javascript函数映射
totals = chartdata.map(function(item){ return item.total})
months= chartdata.map(function(item){ return item.month})
答案 1 :(得分:0)
与其他建议相比,这有点“幻想”,但这不是最有效的解决方案。如果性能是一个问题,那么您将需要使用其他人发布的地图解决方案。
// Your array.
const chartdata = [
{"total":3,"month":9},
{"total":5,"month":5},
{"total":9,"month":2},
{"total":4,"month":1}
];
// Easy way to get each set of values.
const [totals, months] = chartdata.reduce((current, next) => {
return [
current[0].concat(next.total),
current[1].concat(next.month)
];
}, [[], []]);
console.log('Totals:', totals);
console.log('Months:', months);