Reputation: 103
Here is the code of jQuery that alerts undefined
.
I need to pass the actual_price or base_price in a variable and it gives the the desired result when I pass it like:
parsed.test[indx].base_price
Can anyone tell me how can I use this with variable? Thanks in advance
Example
var data = '{"test":[
{"base_price" : "10"},
{"actual_price" : "20"}
]
}';
var parsed = JSON.parse(data);
var indx = 1;
var str = 'actual_price';
alert(parsed.test[indx].str);
Upvotes: 0
Views: 421
Reputation: 104795
You need bracket notation when trying to get a property by variable:
alert(parsed.test[indx][str]);
Upvotes: 5