0

I need to add the elements of arrays together in order, very similar to the question at [How can I add each element of one array to another one's corresponding element using a ParallelStream?, but I'm using javascript and angular. My arrays can come in with any amount of elements up to 31 (days), but they will all always be the same amount of elements across each data object.

$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"
}

My console output tells me I'm separating the elements correctly (to some extent), but when I run this with the if/else if statement, I crash the browser, so I know I'm screwing something up.

$scope.getTotalChanges = function(){
  var series = $scope.test31days.change_series;
  var arry = [];

  for(var i = 0; i < series.length; i++){
    // console.log('change_series loop #', i);
    // console.log('change_series.data length', series[i].data.length);

    var seriesData = series[i].data;
    // console.log('series[i].name', series[i].name)        
    // console.log('seriesData', seriesData)

    for(var j = 0; j < seriesData.length; j++){
      console.log('For inner #', j);
      console.log('seriesData #', seriesData[j]);

      if (j = 0) {
        arry = seriesData;
      } else if (j > 0) {
        arry[j] += seriesData[j]
      };
    }
  }

  // return series;
  console.log('arry ', arry);
  return arry;
};

My end goal is to have a single array of the data for the 31 (days)

2
  • 4
    Shouldn't if (j = 0) be if (j == 0)? Commented Jul 28, 2014 at 20:54
  • also, try using angular's built in for loop... angular.forEach(series, function(value, index){ ... your logic here ... }); Commented Jul 28, 2014 at 21:06

1 Answer 1

1
var series = $scope.test31days.change_series;
var dataLength = series[0].data.length;
var result = Array.apply(null, new Array(dataLength)).map(Number.prototype.valueOf,0); //initialize result array with zeros

for(var i = 0; i < series.length; i++) {
  for(var j = 0; j < dataLength; j++) {
    result[j] += series[i].data[j];
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried plugging this into my directive, and it appears the var result = new Array(dataLength).map(Number.prototype.valueOf, 0); is returning an array of NaN's. Google search tells me that is a result of an illegal number from Number. Any additional thoughts?
That line should be: var result = Array.apply(null, new Array(dataLength)).map(Number.prototype.valueOf, 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.