Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good Evening All,

My question is regarding, how to create dynamic multiple arrays with different random names.
e.g:

JavaScript
for(var i=0; i<5;i++)
{
    var arr=[];// how to create different arrays in context of this.
}

Please help me.
Posted
Updated 1-Aug-12 0:59am
v2

A very good source on javascript can be found at W3Schools Javascript[^]. Have a look at the Array object: JavaScript Array Object[^].

Cheers!

Manfred
 
Share this answer
 
Try this one..
JavaScript
var randomArrayPrefix = 'sample';
var randomArraySuffix = '_arr';
var totalArrayCount = 5;
for(var i = 0; i < totalArrayCount; i++) {
  var arrName = randomArrayPrefix + i + randomArraySuffix;
  if(typeof window[arrName] == 'undefined') {
    window[arrName] = [];
  }
}
document.write('Object name: Sample0_arr' + '<br/>');
document.write('Object type: ' + typeof sample0_arr + '<br/>');
document.write('Object size: ' + sample0_arr.length + '<br/>');
document.write('<br/>Other arrays are - <br/>');

for(ele in window) {
  if(ele.indexOf(randomArrayPrefix) >= 0) {
    document.write('Object name: ' + ele + '<br/>');
    document.write('Object type: ' + typeof window[ele] + '<br/>');
    document.write('Object size: ' + window[ele].length + '<br/>');
  }
}


Hope this may help you out...

Regards,
Niral Soni
 
Share this answer
 
v4
Comments
nawabprince 2-Aug-12 5:58am    
Thanks alot Sir, As I see this would be helpful for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900