0

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);

1 Answer 1

5

You need bracket notation when trying to get a property by variable:

alert(parsed.test[indx][str]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.