armani
armani

Reputation: 313

Access to nested object via variable index

In my Ajax request the response data is an object like this:

enter image description here

how is possible to access value.

note: idVariable is a variable.

data.test1.idVariable.test2.value

Above code result is: undefined.

Upvotes: 0

Views: 49

Answers (1)

Damien Fayol
Damien Fayol

Reputation: 958

When you are using a variable to name a key in a javascript object you are supposed to use bracket notation. E.g:

var idVariable = 8;
var value = data.test1[idVariable].test2.value;

Otherwise, you are accessing a key names idVariable, instead of 8

Upvotes: 1

Related Questions