$scope.$watch('selectedPoll', function(newValue, oldValue){
if(newValue !== oldValue) {
$scope.arrayResult.q1 = [];
$scope.arrayResult.q2 = [];
$scope.arrayResult.q3 = [];
angular.forEach($scope.selectedPoll.questions, function(value, key) {
if (key) {
$scope.arrayResult = [];
var newArrayYes = [];
var newArrayNo = [];
for (var i = 0; i < $scope.selectedPoll.survey_data.length; i++) {
if ($scope.selectedPoll.survey_data[i][key] === "YES") {
var resultOfqYes = newArrayYes.push($scope.selectedPoll.survey_data[i]);
}
if ($scope.selectedPoll.survey_data[i][key] === "NO") {
var resultOfqNo = newArrayNo.push($scope.selectedPoll.survey_data[i]);
}
}
$scope.arrayResult.push({
value: resultOfqYes, color: "#46BFBD", highlight: "#5AD3D1",
label: "Yes"
});
$scope.arrayResult.push({
value: resultOfqNo, color: "#F7464A", highlight: "#FF5A5E",
label: "No"
});
$scope.arrayResult[key] = $scope.arrayResult;
return $scope.arrayResult[key];
}
});
}
});
});
I am encountering an issue where only the data for the last object in selectedPoll.questions is being returned instead of getting three data sets as intended. Although console.log statements inside the 'if' statement show that it is executed for each object, the angular.forEach method seems to be returning only one dataset. I'm unsure why this discrepancy is occurring and how to fix it to get three separate data sets for each object processed by the angular.forEach method.