I have been trying to combine objects inside an array with same id-value. The array that I have is like this:
[
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 0},
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
I have been trying to find a way to combine these so that the result would be like this:
[
{"id" : "abcd","val1" : 2,"val2": 2, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
Is there some way to do this with underscore.js
?
The standard way to do that is to use an object as map (here
b
) :Here's my tedious way: fiddle: http://jsfiddle.net/hN8b6/5/
Refer the below example
This should do it with underscore's functional approach: