Sorin Adrian Carbunaru
Sorin Adrian Carbunaru

Reputation: 618

JSON - access the field of a field

Let's say I have:

 A = {

       B: {
          key : "value1"
       },

       C: {
          key : "value2"
       }
       ..............
}

How can I get the values of the keys using a loop?

I tried something like:

for(ob in A)
{
    console.log(ob);
    console.log(ob.key);
}

but I get:

B
undefined
C
undefined

Upvotes: 0

Views: 84

Answers (2)

karaxuna
karaxuna

Reputation: 26940

for(var propName in A)
{
    console.log(A[propName].key);
}

popName s are B and C in this case. Code will log result of A["B"] and A["C"]

Upvotes: 1

Paul
Paul

Reputation: 141827

ob holds the property name, not the value.

You want to log A[ob] and A[ob].key.

Upvotes: 5

Related Questions