Javascript reduce() on Object

There is nice Array method reduce() to get one value from the Array. Example:

[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
  return previousValue + currentValue;
});

What is the best way to achieve the same with objects? I’d like to do this:

{ 
    a: {value:1}, 
    b: {value:2}, 
    c: {value:3} 
}.reduce(function(previous, current, index, array){
  return previous.value + current.value;
});

However, Object does not seem to have any reduce() method implemented.

15 Answers
15

Leave a Comment