Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize multiple Javascript arrays in a loop?

Tags:

javascript

Let's say i have a for loop and i want to initialize multiple arrays in that loop. Can it be achieved like this?:

for (var i = 0; i < 5; i++){
  var array+i = [];
}

So that the arrays that will be created are array0,array1,array2,array3,array4?

Any help would be much appreciated :)

like image 503
Welou Avatar asked Dec 23 '10 05:12

Welou


2 Answers

You can use a multi dimensional array to tackle this problem:

for(var i=0;i<5;i++){
  var array[i]=[];
}

which will result in:

array[0] = []
array[1] = []
array[2] = []
array[3] = []
array[4] = []

hope that helps :)

like image 152
Damien-Wright Avatar answered Oct 12 '22 11:10

Damien-Wright


You can achieve something like that using

like image 41
Adriaan Stander Avatar answered Oct 12 '22 10:10

Adriaan Stander