Angular.forEach 只返回一个结果?
Angular.forEach returning only one result?
$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];
}
});
}
});
});
我尝试获取三个数据集 arrayResult.q1 q2 和 q3,selectedPoll.questions 中的每个对象一个,但我只得到第三个,尽管当我将 console.log 放在末尾时if 语句,我看到对于 angular.forEach 方法的每个对象都有单独的 console.log.
我的问题,如果对每个对象执行 "if" 语句(在我的例子中是三次)我用 console.log 证明的,为什么 angular.forEach 方法 return selectedPoll.questions ?
中最后一个对象只有一个数据
那么我应该在哪里修复它以获得三个数据集,每个对象对应一个由 angular.forEach 方法执行的对象?
您正在设置 $scope.arrayResult = [];在每个循环的开始覆盖之前循环中设置的任何内容。
$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];
}
});
}
});
});
我尝试获取三个数据集 arrayResult.q1 q2 和 q3,selectedPoll.questions 中的每个对象一个,但我只得到第三个,尽管当我将 console.log 放在末尾时if 语句,我看到对于 angular.forEach 方法的每个对象都有单独的 console.log.
我的问题,如果对每个对象执行 "if" 语句(在我的例子中是三次)我用 console.log 证明的,为什么 angular.forEach 方法 return selectedPoll.questions ?
中最后一个对象只有一个数据那么我应该在哪里修复它以获得三个数据集,每个对象对应一个由 angular.forEach 方法执行的对象?
您正在设置 $scope.arrayResult = [];在每个循环的开始覆盖之前循环中设置的任何内容。