1

I have an object like this:

let object = {
  seniority: 'director_level',
  'address.long_form': 'Phoenix, AZ, United States'
}

when I go to print address.long_form like this I get an error.

console.log(object.address.long_form)

How do get the value for 'address.long_form'?

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • Try `console.log(object['address.long_form'])`. And better yet, don't name your object fields with dots. – goodvibration Jul 01 '20 at 16:01
  • I was wrong about this one. Here's a link, [duplicate](https://stackoverflow.com/questions/2577172/how-to-get-json-objects-value-if-its-name-contains-dots)? – silencedogood Jul 01 '20 at 16:03

3 Answers3

3

Since address.long_form is in string, you cannot use dot notation instead use this

console.log(object['address.long_form'])
Jibin Thomas
  • 775
  • 3
  • 9
  • 25
2

It will work like this: object["address.long_form"], but just don't use dots in object indexes.

Seabyte
  • 369
  • 1
  • 2
  • 13
1

You can still extract it but you can't use the literal notation, you need to use the [] notation in order to get past the ambiguity.

i.e.

console.log(object['address.long_form'])

Of course better would to just avoid keys in that form, but depends on your data.

paulpdaniels
  • 18,395
  • 2
  • 51
  • 55