javascript - How to operate on object based on string -
js noob here. if have string, how can call attribute object has same name string?
var carrot = { 'vitamina': 150, 'vitaminc': 100 }; var cucumber = { 'vitamina': 10, 'vitaminc': 12 }; var vegetable = 'cucumber' alert(vegetable.vitaminc) // doesn't work
note: simplified version of problem i'm working on, can't alert(cucumber.vitaminc) -- have operate on object based on string
you can have like:
window[vegetable].vitaminc
but highly suggest containing these guys in object not global object window
:
var vegetables = { cucumber : { vitamina: 10, vitaminc: 12 }, carrot : { vitamina: 150, vitaminc: 100 } }; var vegetable = 'cucumber'; alert(vegetables[vegetable].vitaminc);
Comments
Post a Comment