View Full Version : calling an array using a string name

Apr 10th, 2005, 04:04 AM
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;

wasn't accessing the array, until I enclose the string in square brackets like :

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


//EDIT: now looking at it it makes perfect sense to do it like that, but is ther a better way?

Apr 10th, 2005, 04:23 AM
If the array is assigned to a variable, and you're referencing it via [string], then that's about the only way to go...

Apr 10th, 2005, 04:38 AM
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...


Apr 10th, 2005, 04:47 AM
hmm, the String object has a split() method, which returns an array...

Apr 10th, 2005, 04:54 AM
thanks again, but you've lost me there, what are you saying to split?

Apr 10th, 2005, 05:06 AM
From what you said, it seemed like the string was an option value/array.

Apr 10th, 2005, 05:18 AM
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,

Apr 10th, 2005, 05:30 AM
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...

Apr 10th, 2005, 05:46 AM
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,

Apr 10th, 2005, 05:58 AM

btw, that could also work like: var products=[["blah","blah"],["blah"]]; var optionSelected=products[selectObj.selectedIndex];

Apr 11th, 2005, 06:09 AM
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'


See my sig for more info of the square bracket notation.