【发布时间】:2018-05-07 01:49:32
【问题描述】:
我创建的数组是空的。我想推独特的对象。我想使用 for 循环。但首先数组长度为零。所以不适用于循环。我该怎么办?
$scope.arr=[];
$scope.result=[
{category: "Home", categoryInt: 1, categoryPercent:50},
{category: "Office", categoryInt: 1, categoryPercent:25},
{category: "Office", categoryInt: 1, categoryPercent:25},
{category: "Office", categoryInt: 1, categoryPercent:25}
[
for(var z=0; z<$scope.arr.length; z++){
if ($scope.arr[z].percent === $scope.result[a].categoryPercent) {
return;
} else {
$scope.arr.push({category: $scope.result[a].category, percent: $scope.result[a].categoryPercent, categoryInt: $scope.result[a].categoryInt});
}
}
【问题讨论】:
-
能否不将相关对象声明为变量,然后在条件语句中使用 Array.include() 函数作为检查?
-
感谢回答我使用了 Array.include 函数。当循环工作时总是返回 false。
标签: javascript arrays angularjs json