[英]Create and initialize bidimensional array in javascript
I am trying to create and initialize a bidimensional array in javascript inside an AngularJS application as follows: 我试图在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;
}
}
But it doesn't work, What is the right way to do that? 但这是行不通的,正确的方法是什么?
try this: 尝试这个:
$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;
}
}
I'm assuming $scope.arry1[i]
is an array that contain other arrays and is already fill with values. 我假设$scope.arry1[i]
是一个包含其他数组的数组,并且已经填充了值。 So your code should look like: 因此,您的代码应如下所示:
$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;
}
}
'$scope.invalidVote = $scope.arry1;' '$ scope.invalidVote = $ scope.arry1;' declaring "invalidVote" like this ensure it contains the same amount of indexes. 像这样声明“ invalidVote”,以确保它包含相同数量的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.