Let a = {a: 1, b:2}
, whichs shows in console Object {a: 1, b: 2}
.
When I do a.a
I get 1.
When I do a[a]
I get undefined.
Is it normal ?
I'm asking this because I need to get values from dynamic keys. a[product1], a[product2]....
Let a = {a: 1, b:2}
, whichs shows in console Object {a: 1, b: 2}
.
When I do a.a
I get 1.
When I do a[a]
I get undefined.
Is it normal ?
I'm asking this because I need to get values from dynamic keys. a[product1], a[product2]....
Yes, this is normal.
a[a]
is the same as a[a.toString()]
which is the same as a['[object Object]']
and you haven't defined a property with that name in the object.
If you want to use square bracket notation to access a property called a
then you have to pass a string with the value a
: a['a']
or var prop = 'a'; a[prop]
.