What is the best way to calculate the vertical sum of data in an array of arrays?
arrayOfArrays = [{
label: 'First Value',
data: [1, 2, 3, 4, 5, 6, 7, 8]
},
{
label: 'Second Value',
data: [1, 2, 3, 4, 5, 6, 7, 8]
},
{
label: 'Third Value',
data: [1, 2, 3, 4, 5, 6, 7, 8]
}
];
var result = arrayOfArrays.reduce(function(accumulatedArray, currentArray) {
return accumulatedArray.data.map(function(value, index) {
return value + currentArray.data[index];
}, 0);
});
console.log(result)
The expected output is the vertical sum of arrays. [3,6,9,12,15,18,21,24]
An issue has been identified where array1 always returns as undefined.