繁体   English   中英

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

[英]How to sum the same objects in 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},
    ],
  }]

要返回这样的内容:

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},
    ],
  }]

会失去阵列的其他部分吗? (手工很难,因为像上面的例子一样,我有1万多个重复项,并且数组有60万个条目。

您可以使用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) 

您可以将功能用于forEachreduce

这种方法会改变原始数组

 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; } 

如果要创建一个新数组并保留原始数据:

这种方法使用功能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; } 

您可以利用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