Reputation:
I have a text box in which the user enters some strings delimited by comma, and these strings will be split on the front end, and sent to the backend to get some data in the form of JSON.
Here's the catch, when I actually typed the key of the JSON like below, it works.
var price = fun.results.KO;
But, when I tried to use the value of the split list, it kept on giving me error;
list_of_key = ["KO", "OK", "NA"]
fun.results.list_of_key[1];
The error says Uncaught TypeError: Cannot read property '0' of undefined
Where is my mistake? How to fix this?
If this is Python it would have world.
Upvotes: 0
Views: 38
Reputation: 17203
You have to use [] notation for it to work like that.
fun.results[list_of_key[1]];
.list_of_key does not exist as a property on fun.results
Upvotes: 1
Reputation: 44298
I think you want this...
fun.results[list_of_key[1]];
when you do
fun.results.list_of_key[1];
it is expecting a property called list_of_key
on the fun.results
, which doesn't exist, so trying to index it causes the error you are seeing.
Upvotes: 1