1

Possible Duplicate:
How to access object using dynamic key?

I have multiple select with different options, the later options depend on the earlier choices. So I would like to generate options for later select, I am in the middle of doing this, and I have encountered some problems:

function generateOptions(selected) 
  { 
  var jsonObj = ('food':['a','b','c'])
  //able to get selected_value as food  
  var selected_value = selected.options[selected.selectedIndex].value 
  var options = jsonObj.selected_value  
  } 

if I do jsonObj.food, then I can get output in console ['a','b','c']. But if I try to use selected_value, then I get undefined

So the . for retrieving data from an Object only works for absolute values not a var? Anybody can help?

Community
  • 1
  • 1
sammiwei
  • 3,140
  • 9
  • 41
  • 53

1 Answers1

4

Use bracket notation for dynamic property names:

var options = jsonObj[selected_value];
jbabey
  • 45,965
  • 12
  • 71
  • 94