Create js array dynamically?

how can i declare multiple js arrays dynamically? For example, here is what tried, but failed:

<script type="text/javascript"> for (i=0;i<10;i++) { var "arr_"+i = new Array(); } 

Thanks!

+4
source share
3 answers

You were pretty close depending on what you would like to do.

 <script type="text/javascript"> var w = window; for (i=0;i<10;i++) { w["arr_"+i] = []; } </script> 

Will work, what is your intention to use?

+7
source

make an array of arrays:

 var arr = []; // creates a new array .. much preferred method too. for (var i = 0; i < 10; i++) { arr[i] = []; } 
+5
source

You can put them all in an array, for example ...

 var arrContainer = []; for (i=0;i<10;i++) { arrContainer.push(new Array()); } 
+1
source

All Articles