(using Angular) I am working on creating an array of arrays to be represented in a chart. The data values are being generated through the following code snippet:
$scope.getData = function() {
$scope.series.length = 0
$scope.allData.length = 0
var dataArray = [];
var dateArray = ["2015-04-03 00:00:00", "2015-04-04 00:00:00", "2015-04-05 00:00:00", "2015-04-06 00:00:00", "2015-04-07 00:00:00"]
for (var i = 0 ; i < $scope.dataDisplayModel.length ; i++) {
if ($scope.dataDisplayModel[i].checked === true) {
var field = $scope.dataList.fields.indexOf($scope.dataDisplayModel[i].field)
dataArray.length = 0;
for (var j = 0 ; j < 5 ; j++) {
var arrayList = $filter('filter')($scope.dataList.values, dateArray[j], true);
var sum = _.sum(arrayList, field);
dataArray.push(sum);
}
$scope.allData.push(dataArray);
}
}
}
Using the variables:
$scope.allData = the desired output array
dataDisplayModel = an array of objects containing field names and a checked property
$scope.dataList = JSON array containing the original data
However, whenever I push to $scope.allData, it seems to replace the previous arrays with duplicates. So, when checking 2 fields, the result is
$scope.allData = [[ARRAY2],[ARRAY2]]
and with 3 fields selected, it shows
$scope.allData = [[ARRAY3],[ARRAY3],[ARRAY3]]
This behavior of overwriting existing arrays is causing confusion. Any insights on why this might be happening would be greatly appreciated.