Reputation: 618
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
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
Reputation: 141827
ob
holds the property name, not the value.
You want to log A[ob]
and A[ob].key
.
Upvotes: 5