0

I am trying to create and initialize a bidimensional array in javascript inside an AngularJS application as follows:

$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?

1
  • I presume $scope.arry1 exists and is an array? Commented Mar 1, 2016 at 22:10

2 Answers 2

1

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;
    }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

$scope.arry1[i].arry2.length will not work. It should read $scope.arry1[1].length - assuming arry1 is an array of arrays and not an array of objects that all have a property arry2 that is an array
This is something only the original poster can answer
You are right. It wouldn't be a 2 dimensional array otherwise, though.
1

I'm assuming $scope.arry1[i] is an array that contain other arrays and is already fill with values. 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;' declaring "invalidVote" like this ensure it contains the same amount of indexes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.