这种二重数组如何把price相加到一起得出总和,
var price = []
for (var i = 0; i < res.data.orderlist.length; i++) {
for (var j = 0; j < res.data.orderlist[i].length; j++) {
price.push(res.data.orderlist[i][j].price);
}
}
const sums = res.data.orderlist[i][j].reduce(
(a, c) => a + Number(c.price),
0
)
console.log(sums)
这是小白自己写的代码 实在写不动了 ,麻烦各路大神指点一下。
var order = []
for (var i = 0; i < res.data.orderlist.length; i++) {
for (var j = 0; j < res.data.orderlist[i].length; j++) {
order.push(res.data.orderlist[i][j]);
}
}
that.setData({
order:order
})
setTimeout(function () {
const sum = that.data.order.reduce(
(a, c) => a + Number(c.price*that.data.intro),
0
)
that.setData({
sum: parseFloat(sum.toFixed(2))
})
}, 500);
解决了 先push出数组,再相加