我想在packings
数组中得到我的sku
数组的总数。我有map
ped和reduce
d数组,但它给了我一个undefined
输出。
sku: [
{
id:1,
value:0,
packings: [
{
id: 1,
cost: 0,
code:'',
pieces: 0,
size:0,
total:0
},
],
},
],
这是我的代码:
let result = this.sku
.map ( (obj,index) => {
parseFloat(obj.packings.total);
console.log(obj.packings.total)
})
.reduce( (total,current) => {
total+current;
})
return this.fixFourDecimal(result);
请注意,sku
和packings
是动态的,用户可以在一个packings
中添加/乘以sku
,并且可以添加许多sku
s。
如果你在箭头函数{}
中使用花括号=>
它不会让你隐式返回 - 你必须使用return
关键字或重构你的函数:
let result = this.sku
.map((obj, index) => {
console.log(obj.packings.total);
return parseFloat(obj.packings.total);
})
.reduce((total, current) => total + current);