I'm looking to sum positions in an object of arrays. Essentially, each number represents a yearly amount for an expense line item, and I need to sum these up for each year. The object looks like this (see below) and the result should be an array which = [1500, 1900, 2400] given the object below.
x = [{
"HVAC": [100, 200, 300, 400],
"foundation": [500, 600, 700, 800],
"roof": [900, 1000, 1100, 1200]
}];
If the arrays weren't named, and instead looked like:
x = [
[100, 200, 300, 400],
[500, 600, 700, 800],
[900, 1000, 1100, 1200]
];
Then this function would do what I want (thanks to this post Sum array of arrays (matrix) vertically efficiently/elegantly):
$scope.reserveTotals = x.reduce(function(a, b) {
return a.map(function(v, i) {
return v + b[i];
});
});
As it stands, I need to keep the original structure and am just struggling to adapt that function do what I need. Any help appreciated!