0

I have an array that holds several values. I have several other areas, each with a different number instead of the

var u3s0A = ["Ques De Ti", "Encina", "Renaissance", "Syllabic", "Polyphonic", "None"];

However, I was unable to figure out how to refer to the arrays dynamically. I tried doing:

alert(u3s + randomNumber + A[p]);

but only got errors. I realize that if i do

alert('u3s' + randomNumber + 'A'[p]);

it outputs the correct array name, but it is then transformed to a string and when I index it:

var arrayHolder = 'u3s' + randomNumber + 'A';
alert(arrayHolder[0]);

I get the first number in the array name (u), not the first item in the array.

Any help would be appreciated!

Thanks so much for your time.

PSL
  • 123,204
  • 21
  • 253
  • 243
nman
  • 57
  • 1
  • 8

2 Answers2

4

Try this way, creating a temporary object and set the arrays as properties of the object and then access it using the bracket notation with the constructed property name:

 var ob = {}; 
 ob.u3s0A = ["Ques De Ti", "Encina", "Renaissance", "Syllabic", "Polyphonic", "None"];
 ob.u3s1A = ["Ques De Ti", "Encina", "Renaissance", "Syllabic", "Polyphonic", "None"];
 .....

and then

 alert(ob['u3s' + randomNumber + 'A'][p]);

If this is in global scope and if you are in browser you can access it with window object with the same way as above instead of the temp object ob.

Demo

PSL
  • 123,204
  • 21
  • 253
  • 243
0

Try something like this:

window['u3s' + randomNumber + A[p]]
Ankit Tyagi
  • 2,381
  • 10
  • 19