【问题标题】:Loop through arrays of objects and return sum by ID reference循环遍历对象数组并按 ID 引用返回总和
【发布时间】:2020-04-08 08:01:59
【问题描述】:

我正在获取一个包含一组对象的对象,并且我正在尝试遍历它们以通过 id 引用对数据求和。

如果我有这样的功能,这可能比解释更容易展示......

let projectArray = this.projects
    projectArray.forEach(function (el) {
        console.log(el.categoriesTotal)
    })

我得到了一个很好的返回数组,其中包含我所追求的对象,看起来像这样.....

[
  { _id: 6, total: 4478.4 },
  { _id: 1, total: 110248.13 },
  { _id: 7, total: 663695.1 }
]
[
  { _id: 7, total: 31278 },
  { _id: 1, total: 67174.66 },
  { _id: 4, total: 3712.8 },
  { _id: 8, total: 670 }
]
...

我想要做的是通过 id 引用返回总和,例如

_id: 1, total: 177422.79,
_id: 6, total: 4478.4

我认为我想要的方法是“减少”,但我尝试跟随this answer,但我收到一个错误,告诉我“减少不是函数”,可能是因为我试图“减少”不止一个数组。

如何从这些数组返回总和?

【问题讨论】:

  • 那么你有一个数组数组?
  • 您是否尝试从二维数组生成单个数组并使用reduce?
  • @Mulli 我没有尝试生成单个数组,不(不确定如何)
  • 只需对您的 projectArray 使用 flat() 方法,以便它创建新数组,其中所有子数组元素递归连接到指定深度,然后您可以使用 reduce 来总结值

标签: javascript arrays node.js


【解决方案1】:

const data = [
  [
    { _id: 6, total: 4478.4 },
    { _id: 1, total: 110248.13 },
    { _id: 7, total: 663695.1 }
  ],
  [
    { _id: 7, total: 31278 },
    { _id: 1, total: 67174.66 },
    { _id: 4, total: 3712.8 },
    { _id: 8, total: 670 }
  ]
];

let temp = {};
data.forEach( arrayOfObjects => {
  arrayOfObjects.forEach( obj => {
    if(temp[obj._id] != null) {
      temp[obj._id] += obj.total
    }else{
      temp[obj._id] = obj.total
    }
  })
})
let result = [];
for ( [key,value] of Object.entries(temp) ){
  result.push({ _id: key*1, total: value })
}

console.log(result)

【讨论】:

    【解决方案2】:

    您应该能够使用Array.flat() 来展平数组,然后使用reduce 来获得所需的结果,例如

    let a = [[
      { _id: 6, total: 4478.4 },
      { _id: 1, total: 110248.13 },
      { _id: 7, total: 663695.1 }
    ],
    [
      { _id: 7, total: 31278 },
      { _id: 1, total: 67174.66 },
      { _id: 4, total: 3712.8 },
      { _id: 8, total: 670 }
    ]];
    
    let result = Object.values(a.flat().reduce((map, r) => { 
        if (!map[r._id]) map[r._id] = { _id: r._id, total: 0};
        map[r._id].total += r.total;
        return map;
    }, {}));
    
    console.log(result);

    【讨论】:

    • 谢谢,如何将数组放入数组?当我将它们从 forEach 记录到控制台时,它们是分开的。
    • 您可以使用非常方便的 array.flat() 将一组数组展平为一个连续的数组。
    【解决方案3】:

    您可以使用Array.prototype.flat 来展平您的数组数组,然后只使用reduce 来聚合总和。

    var projectArray = [[
      { _id: 6, total: 4478.4 },
      { _id: 1, total: 110248.13 },
      { _id: 7, total: 663695.1 }
    ],
    [
      { _id: 7, total: 31278 },
      { _id: 1, total: 67174.66 },
      { _id: 4, total: 3712.8 },
      { _id: 8, total: 670 }
    ]];
    
    var result = projectArray.flat().reduce( (acc,i) => {
       if(acc.hasOwnProperty(i._id))
           acc[i._id] += i.total;
       else
           acc[i._id] = i.total;
       return acc;
    },{});
    console.log(result);

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 2016-10-24
      • 2021-10-13
      • 2012-03-10
      • 1970-01-01
      • 2019-11-20
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多