i have an array like this
[
{
item_guid: \"57e7a1cd6a3f3669dc03db58\"
quantity:3
},
{
item_guid: \"57e77b06e0566d496b51fed5\"
quantity:
You can
item_guid
Something like this:
var result = _.chain(data)
.groupBy('item_guid')
.map(function(objects, guid) {
return {
item_guid: guid,
quantity: _.sumBy(objects, 'quantity')
};
})
.value();
And a demo
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);