3

I have a string variable which contains the name of an array. What I'd like to do is access an element of that array. And write it to another variable. How can I do this?

var sample = new Array();
sample[0] = 'one';
sample[1] = 'two';

var arrayname = "sample";
var number = arrayname[1];  ///  Something like this, although I realize this doesn't work for obvious reasons...

I've read other questions in regards to using an variable value as a function, but I didn't understand the approach or even if this would work for the above situation.

Thanks in advance!!

user1145643
  • 896
  • 5
  • 15
  • 26
  • 2
    See this: http://stackoverflow.com/questions/8588307/calling-a-jquery-function-named-in-a-variable/8588392#8588392 – Andreas Louv Jan 12 '12 at 14:02

7 Answers7

3

This should do it:

    var sample = new Array();
    sample[0] = 'one';
    sample[1] = 'two';

    var arrayname = "sample";
    var number = window[arrayname][1];
    alert(number);
Skyrim
  • 865
  • 5
  • 6
  • 1
    But only if you run it in window scope, not inside a function. – Steven de Salas Jan 12 '12 at 14:17
  • definitely...as far as I know, if you created a local variable within a function, there is no way to get it with just a string name. If a JS Super Guru can show me otherwise, I'd really like to know!! – Skyrim Jan 12 '12 at 14:28
2

There's no universal way in JavaScript to do exactly what you've got set up there, but you can use string variables to access object properties in a dynamic way.

var objArr = { array1: [], array2: [] };

Now you can use a variable with the value "array1" or "array2" to get at those arrays:

var name = "array2";
objArr[name].push(14);

What you cannot do in JavaScript is access local variables by a dynamic name, or indirectly if you prefer. You can access global variables that way if you have a name for the global context, which in a browser is the window object:

window[ name ].push(17);

However there's no way to get a similar name for a local scope.

edit — @Neal points out (and is downvoted mercilessly) that eval() can do what you want, but a lot of people recommend staying far away from eval() unless it's absolutely unavoidable (which is really rare). I've trained myself to ignore it so well that I always forget about it when questions like this are asked (which is, oddly, quite often on SO, though in my programming practice I never find myself wanting to do this).

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Technically, eval should be able to do *exactly* what he wants but I perfectly understand why you wouldn't want to tell him that :) – hugomg Jan 12 '12 at 14:03
  • @missingno as I said in my edit, I actually never think about `eval()` so I simply forgot :-) – Pointy Jan 12 '12 at 14:08
  • @Pointy i even said in my post that it was **not** the best approach. I should have bolded it :-P – Naftali Jan 12 '12 at 16:24
0

You could use something like:

var arrayName = "sample",
    number = window[arrayname][1];
jabclab
  • 14,786
  • 5
  • 54
  • 51
0

I mean you can use eval, but it is not the best approach:

eval("var arrayname = sample");
var number = arrayname[1];  

Why not just do:

var arrayname = sample;
var number = arrayname[1];  
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • I don't like `eval()` any more than anybody else but this doesn't really deserve a downvote; it's technically correct :-) – Pointy Jan 12 '12 at 14:06
0

If it's in the global scope, you can access your array by doing

var sample = new Array();
sample[0] = 'one';
sample[1] = 'two';
var arrayname = "sample";
var number = (window[arrayname])[1]
Grooveek
  • 10,046
  • 1
  • 27
  • 37
0
var sample = new Array();
sample[0] = 'one';
sample[1] = 'two';
var obj = {"sample": sample}

var arrayname = "sample";
var number = obj[arrayname][1];
itea
  • 444
  • 3
  • 5
0

Out of my head...

First declare some global variable

var buf;

Then

var sample = new Array();
sample[0] = 'one';
sample[1] = 'two';

var arrayname = "sample";
eval('buf=' + arrayname + ';');
var number = buf[1];

Damn undesirable way but does what you want...

Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68