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 11 of 11
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    calling an array using a string name

    I have a bit of a problem, fixed it but it just looks wrong so I wanted to throw it up here...

    I have 3 arrays,
    meat = ['ham','pork','chicken'];
    dairy = ['milk','cheese','butter'];
    veg = ['potato','carrot','onion'];

    and I have a select menu with an onchange that returns either meat, dairy or veg.

    depending on what gets returned I want to build some dom elements using the matching array but: selectresult=myform.options[myform.selectedIndex].value;
    newhtmlelm0=selectresult[0];

    wasn't accessing the array, until I enclose the string in square brackets like :
    newhtmlelm0=[selectresult][0];

    my question is : if stringvar = "meat" , is using [stringvar][0] the correct way to access the array named meat?

    cheers,

    dave
    //EDIT: now looking at it it makes perfect sense to do it like that, but is ther a better way?
    Last edited by david_currey; 04-10-2005 at 04:10 AM. Reason: doesn't look so strange anymore

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If the array is assigned to a variable, and you're referencing it via [string], then that's about the only way to go...
    *this message will self destruct in n-seconds*

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the reply,

    I am getting a bit deeper with that method and running into more problems and I think I oversimplified my example a bit 'cos it's still using the string and not the array itself eg [selectresult].length is returning the number of letters in the string, not members of the array.

    I don't even need to do this 'cos I can change other code I just realized I didnt know how to call an array from a returned string...

    confused,

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm, the String object has a split() method, which returns an array...
    *this message will self destruct in n-seconds*

  • #5
    New Coder
    Join Date
    May 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks again, but you've lost me there, what are you saying to split?

  • #6
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From what you said, it seemed like the string was an option value/array.
    *this message will self destruct in n-seconds*

  • #7
    New Coder
    Join Date
    May 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i've confused you, sorry

    what I want to do do is have a function use an array to make some html when an onchange event happens to my select menu. But if I have n options in my select, I will also have n arrays and want to use the array that corresponds with what ever is chosen from the select. eg if select value is meat then use the meat array to make the html,

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's what I thought initially; so if the array is stored in a global variable, then window["meat"] would get the object, if local, eval("meat") would, or you can store the arrays in an object literal, and retrieve them by property name:
    var products = {meat:['ham','pork','chicken'],dairy:['milk','cheese','butter']};
    products["dairy"][1] would be cheese...
    *this message will self destruct in n-seconds*

  • #9
    New Coder
    Join Date
    May 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my problem is that I am trying to access the dairy array with the value from a select menu and it didn't work defining my array like var onearray=["hello"] but storing the arrays in an object literal, and retrieving them by property name works a treat like products[resultvarfromselect][1]

    thanks for the help,

  • #10
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure...
    btw, that could also work like: var products=[["blah","blah"],["blah"]]; var optionSelected=products[selectObj.selectedIndex];
    *this message will self destruct in n-seconds*

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Using your old implementation

    meat = ['ham','pork','chicken'];
    dairy = ['milk','cheese','butter'];
    veg = ['potato','carrot','onion'];

    the array can be accessed through the window object since the arrays are declared global (I assumed it is). So if you have this and the selected item returns 'meat', the alert displays 'ham'

    selectresult=myform.options[myform.selectedIndex].value;
    alert(window[selectresult][0]);


    See my sig for more info of the square bracket notation.
    Last edited by glenngv; 04-11-2005 at 06:11 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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