PDA

View Full Version : [SOLVED] variable access to a javascript array



maclenin
February 17th, 2016, 06:25 PM
Is there a way to access the value of a javascript array using variables?


var a;

var b;

var ab = [1, 2, 3];

var c = a + b;

alert(c);

alert(c); yields NaN and not 1, 2, 3!

Is there a way to combine a and b to create a valid reference to the array ab?

Thanks for any guidance!

DaviesX
February 20th, 2016, 11:17 AM
Sorry, not clear of what you are saying and what you want to achieve. What are a and b? And what you want them to refer to in your example? Both be arrays or strings?

maclenin
February 20th, 2016, 11:47 AM
Essentially, this is what I was / am trying to achieve:


var ab = [1,2,3];

var c = window["a" + "b"];

alert(c);

...yielding 1,2,3 - with thanks (http://stackoverflow.com/questions/8836265/using-a-variable-value-to-call-an-array-element)!