在数组Javascript中获取数组的减少量

问题描述 投票:-2回答:1

我想在packings数组中得到我的sku数组的总数。我有mapped和reduced数组,但它给了我一个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);

请注意,skupackings是动态的,用户可以在一个packings中添加/乘以sku,并且可以添加许多skus。

javascript arrays function ecmascript-6
1个回答
2
投票

如果你在箭头函数{}中使用花括号=>它不会让你隐式返回 - 你必须使用return关键字或重构你的函数:

let result = this.sku
  .map((obj, index) => {
    console.log(obj.packings.total);
    return parseFloat(obj.packings.total);
  })
  .reduce((total, current) => total + current);
© www.soinside.com 2019 - 2024. All rights reserved.