4

I have an array of arrays.

I am trying to write code in javascript for following psedocode.

Take the length of array of arrays

take out each array from array and assign it to new array 

like as following which is not correct.

    for (var i = 0 ; i < $scope.Permissions.length; i++) 
        var arr + [i] = $scope.Permissions[i];

Any help?

2
  • Are you trying to concatenate the individual arrays to a single, "long" array? Commented Jun 28, 2015 at 7:24
  • 1
    you can't dynamically create variables like that. you can dynamically assign properties to an object (the scope object for example like described in one of the answers below). Commented Jun 28, 2015 at 13:34

2 Answers 2

1

You can also use array's builtin forEach method:

$scope.Permissions.forEach(function (arr, i) { $scope['arr' + i] = arr });

Sign up to request clarification or add additional context in comments.

Comments

0

In your case it makes sense to create bunch of new arrays as properties of the $scope object:

for (var i = 0 ; i < $scope.Permissions.length; i++) {
    $scope['arr' + i] = $scope.Permissions[i];
}

console.log($scope.arr0, $scope.arr1);

3 Comments

i got this error in console: Cannot read property '0' of undefined
What is $scope.Permissions? Check console.log($scope.Permissions).
@ dfsq. My mistake it works fine as u suggested. thanks

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.