Skip to content Skip to sidebar Skip to footer

Why I Can't Access Json Dictionary Element Using Dot Notation?

I have a following JSON representation: var collectionCopy = JSON.parse(JSON.stringify( { 1 : { 2: '2' } } )); Why cant I access key '2' using dot notation (i.e.

Solution 1:

You can use the dot notation for accessing an object's properties only on a valid identifiers in the language.

And since numbers (or anything that starts with a number) are not a valid identifiers you can access it (as a property of an object) only with the bracket notation.

Solution 2:

This is because the keys are strings not actual numbers:

to access it use:

collectionCopy[1][2]

or

collectionCopy['1']['2']

Relevant docs on accessing properties

Post a Comment for "Why I Can't Access Json Dictionary Element Using Dot Notation?"