Skip to content Skip to sidebar Skip to footer

Javascript: Array Name As String; Need It To Reference The Actual Array

I have to 'get at' an array, but all I have is a string that matches the array name. Obviously this kind of thing won't work, but it shows what I'm trying to do: var arrayname = n

Solution 1:

It's very simple.

var storage = {};
storage.arrayname = [1, 2, 3];
alert(storage["arrayname"].join(','));

Polluting the global namespace is strongly discouraged. I would strongly advise you to refrain from using the window object for this purpose. Read HERE for more details.

Solution 2:

Try using the window object to retrieve it if it is defined in the window context.

var array = window["arrayname"]

Solution 3:

You can use

array = window['arrayname'];

Post a Comment for "Javascript: Array Name As String; Need It To Reference The Actual Array"