在javascript中创建并初始化二维数组

时间:2016-03-01 21:58:50

标签: javascript arrays angularjs

我正在尝试在AngularJS应用程序中的javascript中创建和初始化二维数组,如下所示:

$scope.invalidVote = [];
for (var i = 0; i < $scope.arry1.length; i += 1) {
    $scope.answersCount[i] = $scope.arry1[i].arry2.length;

    for(var j = 0; j < $scope.arry1[i].arry2.length; j += 1) {
        $scope.invalidVote[i][j] = false;
    }
}

但它不起作用,这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

试试这个:

$scope.invalidVote = [];
for (var i = 0; i < $scope.arry1.length; i++) {
    $scope.answersCount[i] = $scope.arry1[i].arry2.length;
    $scope.invalidVote[i] = [];

    for(var j = 0; j < $scope.arry1[i].arry2.length; j++) {
        $scope.invalidVote[i][j] = false;
    }
 }

答案 1 :(得分:1)

我假设$scope.arry1[i]是一个包含其他数组且已填充值的数组。 所以你的代码应该是这样的:

$scope.invalidVote = $scope.arry1;
for (var i = 0; i < $scope.arry1.length; i += 1) 
  {
    $scope.answersCount[i] = $scope.arry1[i].length;
    for(var j = 0; j < $scope.arry1[i].length; j += 1)
      {
        $scope.invalidVote[i][j] = false;
      }
  }

&#39; $ scope.invalidVote = $ scope.arry1;&#39;声明&#34; invalidVote&#34;像这样确保它包含相同数量的索引。