Take the 2-minute tour ×

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?

share|improve this question

marked as duplicate by Felix Kling, Álvaro G. Vicario, casperOne Mar 15 '12 at 12:47


var jsonObj = ('food':['a','b','c']) doesn't look like valid JavaScript btw. –  Felix Kling Mar 14 '12 at 20:00
add comment

1 Answer

up vote 3 down vote accepted

Use bracket notation for dynamic property names:

var options = jsonObj[selected_value];
share|improve this answer
add comment
