html - I have a JSON Object in JavaScript and want to access a key from a String variable -
this question has answer here:
in javascript, have json object. json object has many keys , need access 1 stored in string using dot operator not sure how this.
for example, if have following json code:
"values" : [ { "prop0" : "h", "prop1" : "pizza", "prop2" : "2014-06-24t15:58:50z", "prop3" : "" }, { "prop0" : "paa", "prop1" : "cat", "prop2" : "2014-06-24t15:58:16z", "prop3" : "mouse" } ] and want access prop1 use following javascript code:
values[0].prop1; this works long know property called prop1. don't know properties called. have string represents what, in case, prop1 or whatever current property is, don't know how use dot operator string.
i like:
values[0].mystring; but not work.
is there way trying achieve?
you can treat javascript object associative array or dictionary passing key lookup:
values[0][mystring]
in following code,
var mystring = 'prop1'; var val1 = values[0][mystring]; var val2 = values[0].prop1; val1 , val2 both hold value of 'pizza'
Comments
Post a Comment