0

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.

asked Sep 15, 2016 at 23:44

2 Answers 2

1

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

answered Sep 15, 2016 at 23:46
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Sep 15, 2016 at 23:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.