Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2009, 11:28 PM   PM User | #1
mmseng
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mmseng is an unknown quantity at this point
Variable multidimensional array names with window[]?

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:

Code:
// 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

Last edited by mmseng; 05-06-2009 at 02:13 AM..
mmseng is offline   Reply With Quote
Old 05-06-2009, 12:19 AM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Is there a particular reason (except for its name) why getArrayName has to return a string and not the appropriate array itself?
venegal is offline   Reply With Quote
Old 05-06-2009, 12:49 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I'm just curious why anybody would *WANT* to use window[ ] for anything like this.

Note to original poster:

*UNLESS* you do
Code:
var sub = "sub";
Then
Code:
myArray[sub]
will never be the same thing as
Code:
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?
Old Pedant is offline   Reply With Quote
Old 05-06-2009, 02:12 AM   PM User | #4
mmseng
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mmseng is an unknown quantity at this point
Quote:
Originally Posted by venegal View Post
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:
Code:
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.

Quote:
Originally Posted by Old Pedant View Post
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.

Quote:
Originally Posted by Old Pedant View Post
Note to original poster:...
That was a typo,
Code:
// The specific sub-array I want is not known immediately, but will be e.g.
// 'myArray' or 'myArray[sub]'
should have read
Code:
// 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.

Last edited by mmseng; 05-06-2009 at 02:26 AM..
mmseng is offline   Reply With Quote
Old 05-06-2009, 02:27 AM   PM User | #5
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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?
venegal is offline   Reply With Quote
Old 05-06-2009, 08:31 AM   PM User | #6
mmseng
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mmseng is an unknown quantity at this point
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.
mmseng is offline   Reply With Quote
Old 05-06-2009, 11:37 AM   PM User | #7
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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:
Code:
element.innerHTML = "<input id='id1' onclick='doSomething(\"" + arrayName + "\");' />";
venegal is offline   Reply With Quote
Old 05-07-2009, 07:39 PM   PM User | #8
mmseng
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mmseng is an unknown quantity at this point
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:

Code:
// 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']);
mmseng is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.