Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Is there a particular reason (except for its name) why getArrayName has to return a string and not the appropriate array itself?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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?

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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?

  • #6
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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 + "\");' />";

  • #8
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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']);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •