0

I am iterating through a hashmap, I can see in the console that both the hashmap r and one if its keys k is defined (as well as the corresponding value v):

r
Object {sent020: Object, sent005: Object, sent007: Object, sent002: Object, sent006: Object…}
k
"sent020"
v
Object {org: "someorg", uptime: Object, ip: "10.10.01.1", countryCode: "FR", memory: Object…}

I cannot however access r.k:

r.k
undefined

I was expecting to find that r.k is v. Why isn't it so?

Sorry if this is obvious, I am new to Javascript and the equivalent code in Python yields the expected result:

>>> r = {"sent20": {"org": "someorg", "ip": "10.10.01.1"}}
>>> for k,v in r.items():
...     print(k, v, r[k])
...
sent20 {'org': 'someorg', 'ip': '10.10.01.1'} {'org': 'someorg', 'ip': '10.10.01.1'}
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • I would think you'd expect `r.sent020` to access the property `sent020` of `r`. Why then do you expect `r.k` to access the property `sent020`, even though there's no discernible difference in syntax? – deceze Nov 08 '16 at 14:29
  • @deceze: since the value of `k` is `sent020` then why `r.k` isn't the same as `r.sent020`? This is why I added a Python code to hopefully have an explanation of the difference. – WoJ Nov 08 '16 at 14:30
  • r.k is r["k"] and not r[k], same in both languages – fafl Nov 08 '16 at 14:31
  • So any time you have a variable in scope that matches a property name, the variable value is used instead of the property name you think you're writing? Doesn't sound very sane. – deceze Nov 08 '16 at 14:38

0 Answers0