...

View Full Version : Variable multidimensional array names with window[]?



mmseng
05-06-2009, 12:28 AM
Hey I'm trying to use variable variable names using the window[] functionality. It works fine when you literally specify an array's base variable name, but I have a multidimensional array and would like to use window[] to build the name of the specific sub-array I'm looking for, like so:



// here's my multidimensional array
myArray = new Array();
myArray['id'] = 'foo';
myArray['sub'] = new Array();
myArray['sub']['id'] = 'bar';

// The specific sub-array I want is not known immediately, but will be e.g.
// "myArray" or "myArray['sub']"
temp = getArrayName();

document.write(window[temp]['id']);


However if temp is the name of a sub-array (e.g. 'myArray[sub]), and not the base array name(i.e. 'myArray'), then window[temp] evaluates to "undefined".

Can the window[] functionality handle this somehow, or do I have to resort to eval() or something else?

Thanks,
== Matt

venegal
05-06-2009, 01:19 AM
Is there a particular reason (except for its name) why getArrayName has to return a string and not the appropriate array itself?

Old Pedant
05-06-2009, 01:49 AM
I'm just curious why anybody would *WANT* to use window[ ] for anything like this.

Note to original poster:

*UNLESS* you do


var sub = "sub";

Then


myArray[sub]

will never be the same thing as


myArray["sub"]

No, it doesn't matter whether you use " or ' to delineate the string "sub"/'sub'.

But I wouldn't expect it to work, in either case. window[x] is *NOT* the same thing as eval(x). Why don't you want to use eval?

mmseng
05-06-2009, 03:12 AM
Is there a particular reason (except for its name) why getArrayName has to return a string and not the appropriate array itself?

getArrayName isn't a real function, I just represented it that way for simplicity. It's actually getting the variable name from the HTML itself.

I originally passed the variable name into the HTML like so:


element.innerHTML = "<input id='id1' onclick='doSomething(" + arrayName + ");' />";

which prints the array name into the HTML (which could be "myArray" or "myArray['sub']"). And now I am getting it back when the user activates that input.


I'm just curious why anybody would *WANT* to use window[ ] for anything like this.... Why don't you want to use eval?

Because it's extremely useful, and there's no sense in wasting resources and security on eval() when Javascript was nice enough to put all of the variable names in an already existing array for me.


Note to original poster:...

That was a typo,

// The specific sub-array I want is not known immediately, but will be e.g.
// 'myArray' or 'myArray[sub]'
should have read

// The specific sub-array I want is not known immediately, but will be e.g.
// "myArray" or "myArray['sub']"
I fixed it in in the OP.

venegal
05-06-2009, 03:27 AM
But you're passing the actual array into doSomething, and not just a string containing the array name. How do you even get into the situation where you have lost the array and only have the name of the array left?

mmseng
05-06-2009, 09:31 AM
Maybe you didn't read it right. I'm not passing the array into anything. I'm printing the array-name into the HTML of e.g. a button, in the onclick attribute. So when the button is clicked it sends the arrayname back as a string argument to whatever function I'm executing with the onclick attribute.

venegal
05-06-2009, 12:37 PM
It does not. It sends the actual array to your function. If you'd send the array name as a string, your code would look like this:

element.innerHTML = "<input id='id1' onclick='doSomething(\"" + arrayName + "\");' />";

mmseng
05-07-2009, 08:39 PM
Ah, another typo. That's what I get for recreating some example code on the fly in a post.

Anyway, I realized that I don't have to use window.temp in all my subsequent calls, so I went with something like this:



// here's my multidimensional array
myArray = new Array();
myArray['id'] = 'foo';
myArray['sub'] = new Array();
myArray['sub']['id'] = 'bar';

// The specific sub-array I want is not known immediately, but will be e.g.
// "myArray" or "myArray['sub']"
temp = getArrayName();

if(temp is the base array) {
temp = window.temp;
}
else if(temp is the subarray) {
temp = window.temp.['sub'];
}

document.write(window.temp['id']);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum