简体   繁体   English

如何对数组中的相同对象求和

[英]How to sum the same objects in array

It is possible to sum the values of an array if they are the same like this: 如果它们的值相同,则可以对数组的值求和:

var COLLECTION = [
  {
    "coords":[1335,2525],
    "items":[
      {id: "boletus",qty: 1},
      {id: "lepiota",qty: 3},
      {id: "boletus",qty: 2},
      {id: "lepiota",qty: 4},
      {id: "carbonite",qty: 4},
    ],
  },
  {
    "coords":[1532,2889],
    "items":[
      {id: "boletus",qty: 2},
      {id: "lepiota",qty: 6},
      {id: "boletus",qty: 1},
      {id: "lepiota",qty: 4},
      {id: "chamomile",qty: 4},
    ],
  }]

To return something like this: 要返回这样的内容:

var COLLECTION = [
  {
    "coords":[1335,2525],
    "items":[
      {id: "boletus",qty: 3},
      {id: "lepiota",qty: 7},
      {id: "carbonite",qty: 4},
    ],
  },
  {
    "coords":[1532,2889],
    "items":[
      {id: "boletus",qty: 3},
      {id: "lepiota",qty: 10},
      {id: "chamomile",qty: 4},
    ],
  }]

Wihout losing the other parts of the array? 会失去阵列的其他部分吗? (doing by hand is hard because I have more than 10 thousand duplicates like the example above, and the array have 600 thousand entries. (手工很难,因为像上面的例子一样,我有1万多个重复项,并且数组有60万个条目。

You could use map() to create new array and inside reduce() to group items objects by id and sum qty. 您可以使用map()创建新数组,并在reduce()内部使用id和sum数量对items对象进行分组。

 var data = [{"coords":[1335,2525],"items":[{"id":"boletus","qty":1},{"id":"lepiota","qty":3},{"id":"boletus","qty":2},{"id":"lepiota","qty":4},{"id":"carbonite","qty":4}]},{"coords":[1532,2889],"items":[{"id":"boletus","qty":2},{"id":"lepiota","qty":6},{"id":"boletus","qty":1},{"id":"lepiota","qty":4},{"id":"chamomile","qty":4}]}] const result = data.map(function({coords, items}) { return {coords, items: Object.values(items.reduce(function(r, e) { if(!r[e.id]) r[e.id] = Object.assign({}, e) else r[e.id].qty += e.qty return r; }, {}))} }) console.log(result) 

You can use the functions forEach and reduce 您可以将功能用于forEachreduce

This approach mutates the original array 这种方法会改变原始数组

 var COLLECTION = [ { "coords":[1335,2525], "items":[ {id: "boletus",qty: 1}, {id: "lepiota",qty: 3}, {id: "boletus",qty: 2}, {id: "lepiota",qty: 4}, {id: "carbonite",qty: 4}, ], }, { "coords":[1532,2889], "items":[ {id: "boletus",qty: 2}, {id: "lepiota",qty: 6}, {id: "boletus",qty: 1}, {id: "lepiota",qty: 4}, {id: "chamomile",qty: 4}, ], }]; COLLECTION.forEach((o) => { o.items = Object.values(o.items.reduce((a, c) => { (a[c.id] || (a[c.id] = {id: c.id, qty: 0})).qty += c.qty; return a; }, {})); }); console.log(COLLECTION); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

If you want to create a new array and keep the original data: 如果要创建一个新数组并保留原始数据:

This approach uses the function map to create a new "cloned" array. 这种方法使用功能map来创建新的“克隆”数组。

 var COLLECTION = [ { "coords":[1335,2525], "items":[ {id: "boletus",qty: 1}, {id: "lepiota",qty: 3}, {id: "boletus",qty: 2}, {id: "lepiota",qty: 4}, {id: "carbonite",qty: 4}, ], }, { "coords":[1532,2889], "items":[ {id: "boletus",qty: 2}, {id: "lepiota",qty: 6}, {id: "boletus",qty: 1}, {id: "lepiota",qty: 4}, {id: "chamomile",qty: 4}, ] }], result = COLLECTION.map(o => o); result.forEach((o) => { o.items = Object.values(o.items.reduce((a, c) => { (a[c.id] || (a[c.id] = {id: c.id, qty: 0})).qty += c.qty; return a; }, {})); }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You could take the power of Map and render the result by using Array.from with a mapping function which builds new objects for items . 您可以利用Array.from的映射功能来利用Map并渲染结果,该功能为items构建新对象。

 var COLLECTION = [{ coords: [1335, 2525], items: [{ id: "boletus", qty: 1 }, { id: "lepiota", qty: 3 }, { id: "boletus", qty: 2 }, { id: "lepiota", qty: 4 }, { id: "carbonite", qty: 4 }], }, { coords: [1532, 2889], items: [{ id: "boletus", qty: 2 }, { id: "lepiota", qty: 6 }, { id: "boletus", qty: 1 }, { id: "lepiota", qty: 4 }, { id: "chamomile", qty: 4 }] }]; COLLECTION.forEach(o => { var map = new Map; o.items.forEach(({ id, qty }) => map.set(id, (map.get(id) || 0) + qty)); o.items = Array.from(map, ([id, qty]) => ({ id, qty })); }); console.log(COLLECTION); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM