JQuery - show only one if the same id and also add it on weeks [duplicate]
up vote
2
down vote
favorite
1
This question already has an answer here:
Most efficient method to groupby on a array of objects
33 answers
I have this sample data: [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}] I wanted that I will only show one if id is redundant but add all of the weeks that the id's had My Attempt: var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]; var weeks = 0; for (var i=0; i<data.length; i++){ weeks += data[i].weeks; } Desired output [{"id":"2", "w...