JavaScript Object using . to retrieve values [duplicate]

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?

Answers:

Answer

Use bracket notation for dynamic property names:

var options = jsonObj[selected_value];

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.