I am trying to combine the elements of multiple arrays in a specific order, similar to the question found at [How can I add each element of one array to another one's corresponding element using a ParallelStream?. However, my approach involves using javascript and angular. The arrays I am working with may contain up to 31 elements (days), but each data object will always have the same number of elements.
$scope.test31days = {
"change_series": [
{ "data": [0,0,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,0,0],
"name": "EMERGENCY",
"color": "#904040"},
{ "data": [0,1,3,0,0,0,0,1,2,3,3,0,0,0,2,1,1,1,0,0,1,1,3,3,1,0,0,1,2,2,0],
"name": "MINOR",
"color": "#333"},
{ "data": [0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0],
"name": "MAJOR",
"color": "#666"}
],
"change_title": "CHANGES"
}
Upon inspecting the console output, it appears that I am correctly segregating the elements, albeit encountering issues when implementing the if/else if statement. This causes the browser to crash, indicating errors in my code structure.
$scope.getTotalChanges = function(){
var series = $scope.test31days.change_series;
var arry = [];
for(var i = 0; i < series.length; i++){
var seriesData = series[i].data;
for(var j = 0; j < seriesData.length; j++){
if (j === 0) {
arry = seriesData;
} else if (j > 0) {
arry[j] += seriesData[j];
}
}
}
console.log('arry ', arry);
return arry;
};
My objective is to compile a single array containing data for all 31 days.