var data = [ {id:1, qty:100}, {id:2, qty:200}, {id:1, qty:100}, {id:2, qty:200} ];
How to sum this array become to [ {id:1, qty:200}, {id:2, qty:
[ {id:1, qty:200}, {id:2, qty:
Try this:
var sum = []; data.forEach(function(o) { var existing = sum.filter(function(i) { return i.id === o.id })[0]; if (!existing) sum.push(o); else existing.qty += o.qty; });
See Fiddle