合并对象&对单个属性javascript求和

merge object & sum a single property javascript

本文关键字:属性 javascript 求和 单个 对象 合并      更新时间:2023-09-26

我有一个像这样的数组

[
  {
    item_guid: "57e7a1cd6a3f3669dc03db58"
    quantity:3
  },
  {
    item_guid: "57e77b06e0566d496b51fed5"
    quantity:3
  },
  {
    item_guid: "57e7a1cd6a3f3669dc03db58"
    quantity:3
   },
  {
    item_guid: "57e77b06e0566d496b51fed5"
    quantity:3
  }
]

我想从这里把相似的item_guid合并成一个对象,数量像这样总结

[
  {
    item_guid: "57e7a1cd6a3f3669dc03db58"
    quantity:6
   },
  {
    item_guid: "57e77b06e0566d496b51fed5"
    quantity:6
  }
]

我知道这可以实现一些循环,相反,我正在寻找使用lodash或EcmaScript2015代码的解决方案,使我们的生活更轻松

一个简单的方法:

循环遍历数据并创建一个对象,其中key将是唯一id,其值将是quantity。然后循环遍历对象的键并创建对象。

var data = [{ item_guid: "57e7a1cd6a3f3669dc03db58", quantity: 3 }, { item_guid: "57e77b06e0566d496b51fed5", quantity: 3 }, { item_guid: "57e7a1cd6a3f3669dc03db58", quantity: 3 }, { item_guid: "57e77b06e0566d496b51fed5", quantity: 3 }]
var r = {};
data.forEach(function(o){
  r[o.item_guid] = (r[o.item_guid] || 0) + o.quantity;
})
var result = Object.keys(r).map(function(k){
  return { item_guid: k, quantity: r[k] }
});
console.log(result)

一个纯Javascript解决方案,包含item_guid的哈希表。

var data = [{ item_guid: "57e7a1cd6a3f3669dc03db58", quantity: 3 }, { item_guid: "57e77b06e0566d496b51fed5", quantity: 3 }, { item_guid: "57e7a1cd6a3f3669dc03db58", quantity: 3 }, { item_guid: "57e77b06e0566d496b51fed5", quantity: 3 }],
    grouped = data.reduce(function (hash) {
        return function (r, a) {
            (hash[a.item_guid] = hash[a.item_guid] || r[r.push({ item_guid: a.item_guid, quantity: 0 }) - 1]).quantity += a.quantity;
            return r;
        };
    }(Object.create(null)), []);
console.log(grouped);

ES6

var data = [{ item_guid: "57e7a1cd6a3f3669dc03db58", quantity: 3 }, { item_guid: "57e77b06e0566d496b51fed5", quantity: 3 }, { item_guid: "57e7a1cd6a3f3669dc03db58", quantity: 3 }, { item_guid: "57e77b06e0566d496b51fed5", quantity: 3 }],
    grouped = data.reduce((map => (r, a) => {
        map.set(a.item_guid, map.get(a.item_guid) || r[r.push({ item_guid: a.item_guid, quantity: 0 }) - 1]);
        map.get(a.item_guid).quantity += a.quantity;
        return r;
    })(new Map), []);
console.log(grouped);

你可以

  • item_guid对对象进行分组
  • 对结果组中的对象求和
  • 将这些组转换成你想要的格式

像这样:

var result = _.chain(data)
    .groupBy('item_guid')
    .map(function(objects, guid) {
        return {
            item_guid: guid, 
            quantity: _.sumBy(objects, 'quantity')
        };
    })
    .value();

和一个演示

var data = [
  {
    item_guid: "57e7a1cd6a3f3669dc03db58",
    quantity:3
  },
  {
    item_guid: "57e77b06e0566d496b51fed5",
    quantity:3
  },
  {
    item_guid: "57e7a1cd6a3f3669dc03db58",
    quantity:3
   },
  {
    item_guid: "57e77b06e0566d496b51fed5",
    quantity:3
  }
];
var result = _.chain(data)
    .groupBy('item_guid')
    .map(function(objects, guid) {
        return {
            item_guid: guid, 
            quantity: _.sumBy(objects, 'quantity')
        };
    })
    .value();
  
  console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

var groupedItems = [];
var totalItems = [{item_guid: "57e7a1cd6a3f3669dc03db58", quantity:3},{item_guid: "57e77b06e0566d496b51fed5",quantity:3}, { item_guid: "57e7a1cd6a3f3669dc03db58", quantity:3 }, { item_guid: "57e77b06e0566d496b51fed5", quantity:3}];
totalItems.forEach((e) => {
  if (groupedItems.findIndex(x => x.item_guid === e.item_guid) < 0) {
    let totalQuantity = totalItems
                       .filter(x => x.item_guid === e.item_guid)
                       .map(x => x.quantity)
                       .reduce(function(a, b) { return a + b;});
    groupedItems.push({item_guid: e.item_guid, quantity: totalQuantity})
  }
})
console.log(groupedItems);