Created
August 10, 2016 17:40
-
-
Save r-winkler/834b35baf60ed4028f97d2444a2fa01c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [ | |
{ name: "A", value: 10 }, | |
{ name: "B", value: 20 }, | |
{ name: "C", value: 30 }, | |
{ name: "A", value: 40 }, | |
{ name: "B", value: 50 }, | |
{ name: "C", value: 60 }]; | |
var accumulated = arr.reduce(function(accumulator, element) { | |
var currentValue = accumulator[element.name]; | |
if(currentValue !== undefined) { | |
accumulator[element.name] = currentValue + element.value; | |
} | |
else { | |
accumulator[element.name] = element.value; | |
} | |
return accumulator; | |
}, {}); | |
var result = Object.keys(accumulated).map(function(k) {return {name: k, value: accumulated[k]}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment