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:400} ];
Thx.
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:400} ];
Thx.
Try this:
See Fiddle
With
Ramda
:http://jsfiddle.net/4496escu/2/
I didn't get to run this on any data, but the logic seems right. Basically this code goes through your array, and for each unique instance it will store the values into a temporary array and the write over the original array with the results.
You can do it by using the functional way, with the .reduce() and .map() method.
In my example, i've made a function to make the process more generic.
This function takes 3 arguments