JS How to use reduce on array containing multiple numerical values

angularjs, javascript

Solution

var array = [{
  PropertyOne : 1,
  PropertyTwo : 5
},
{
  PropertyOne : 2,
  PropertyTwo : 5
}];
var reducedArray = array.reduce(function(accumulator, item) {
  // loop over each item in the array
  Object.keys(item).forEach(function(key) {
    // loop over each key in the array item, and add its value to the accumulator.  don't forget to initialize the accumulator field if it's not
    accumulator[key] = (accumulator[key] || 0) + item[key];
  });

  return accumulator;
}, {});

Problem

I have an array like this. ``` [{ PropertyOne : 1, PropertyTwo : 5 }, { PropertyOne : 3, PropertyTwo : 5 },...] ``` And I want to end up with an array like this which aggregates all the columns of this array to end up like this. ``` [{ PropertyOne : 4, PropertyTwo : 10 }} ``` If it was a single column I know I could use .reduce but can't see how I could do with multiple columns ?

Original source