1

I'm new to JS and I don't know how to access properties of an object, which are objects themselves. I have the following object(let´s call it 'a'), where I want to access -kk7b9q6FWN1VkCCflEX.name for example.

enter image description here

I tried

console.log(a.-kk7b9q6FWN1VkCCflEX.name)

but it gave me an error, due to '-' being an exception. I know how to get the key names:

Object.keys(a)

but I cant figure out how to access their properties.

Could someone help me out? Actually, I would be fine if someone could give me a "keyword" I can google on for this topic(because I don't even know what to google). Googling 'Accessing objects inside objects' didn´t help me :(

nem035
  • 34,790
  • 6
  • 87
  • 99
Faizy
  • 299
  • 2
  • 12

1 Answers1

2

The keyword you're looking for is property accessor.

Object access in JavaScript can be done via .property or ['property'].

In your case, ['-kk7b9q6FWN1VkCCflEX'] will work due to syntactic limitations in using the . where the property name has to be a valid identifier.

The particular problem for -kk7b9q6FWN1VkCCflEX is the - sign.

nem035
  • 34,790
  • 6
  • 87
  • 99
  • Thanks a lot! Assuming i Would have `kk7b9q6FWN1VkCCflEX` instead of `-kk7b9q6FWN1VkCCflEX` ,accessing via the dot-nation should work, right? `a.kk7b9q6FWN1VkCCflEX.name` ? Sorry, will have to find out how to format comments, will edit asap. EDITED. – Faizy May 16 '17 at 19:14