2

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 = new Array(1, 2, 3);
var array = 'arrayname';

Alert(array[0]);

Of course, the example above yeilds 'a' instead of 1, like I'd need.

The background is that I'm working with a Hyperion Business Intelligence dashboard, where which array used, is determined by a substring of the button's name that was used to call it.

  • Where is the array referenced? Is it a property of some object. I hope you're not telling me that it's a global variable! – Brandon Apr 29 '13 at 15:08
  • 1
    This will help: http://stackoverflow.com/questions/1664282/javascript-refer-to-a-variable-using-a-string-containing-its-name – tymeJV Apr 29 '13 at 15:08
  • Thanks for the pageslap tymeJV :) I did search, but the appropriate keywords eluded me, and the automatic results didn't have it. It's pretty much my same question. In my mockup, it looks like eval() is what I was looking for, but I need to put it into practice. Thanks! – undrline - Reinstate Monica Apr 29 '13 at 15:19
  • @epascarello they're trying to do operations with it and then assign a value to an existing object. It is similar, but not a duplicate. – undrline - Reinstate Monica Apr 29 '13 at 15:28
  • Don't use `eval`. It's just not needed. If this is a global variable, use the `window` solutions below. If not, then don't use a variable to store your Array. Use the variable to store and object, and have the object store the Array so that you can properly use a string to fetch it. –  Apr 29 '13 at 15:41
  • ...like alex23's solution –  Apr 29 '13 at 15:41
  • 2
    @EdwardD it is the same thing. `alert(window[array][0]);` There is about 50 dupes with the same question. I just picked one of them. – epascarello Apr 29 '13 at 15:44
  • Not sure why people are so down on eval(). It seems to have something to do with whether or not my use is global or local. It's a global function, but it's private to that function, fwiw. eval() is working perfectly for my needs. It may not be the correct solution, but I don't see it creating problems further down the line. Again, this is not for web use, but for dashboarding within a business intelligence client that happens to have its own "take" on javascript. Don't know whom to mark as answered, since none of these will work within my environment. – undrline - Reinstate Monica Apr 29 '13 at 17:26

3 Answers3

6

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.

Community
  • 1
  • 1
flavian
  • 28,161
  • 11
  • 65
  • 105
  • I think this is similar to @pscoder's answer, but creates something other than window to use. Still seems awkward and complicated for my purposes, but maybe I'll come back to it. – undrline - Reinstate Monica Apr 29 '13 at 15:31
  • 1
    @EdwardD: It's neither awkward nor complicated. It's just a simple object, and is the proper solution. You can shorten it to `var obj = {arrayname: [1,2,3]};` Then access as `obj[array][0];` –  Apr 29 '13 at 15:43
2

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

var array = window["arrayname"]
Ian
  • 50,146
  • 13
  • 101
  • 111
PSL
  • 123,204
  • 21
  • 253
  • 243
0

You can use

array = window['arrayname'];
walid
  • 494
  • 3
  • 16