-2

I'm trying to access the value of a key inside a Javascript object. My object currently looks like:

const options = {
  "account.country": getCountry,
  "account.phone": getPhone,
}

When I console.log(options), it shows the whole object. But, when I try

console.log(options.account) // undefined, 
console.log(options.account.country) // error. 

I don't understand why. I also tried:

 const parsedObj = JSON.parse(options);
 console.log(parsedObj);

But it just returns

'Unexpected token o in JSON at position 1'

JronCh
  • 163
  • 1
  • 3
  • 11
  • 1
    what is `getCountry` and `getPhone` ? – Kunal Mukherjee May 20 '19 at 13:21
  • 2
    `options["account.country"]`? –  May 20 '19 at 13:21
  • 3
    Possible duplicate of [How to get JSON objects value if its name contains dots?](https://stackoverflow.com/questions/2577172/how-to-get-json-objects-value-if-its-name-contains-dots) and [How to access object properties containing special characters?](https://stackoverflow.com/questions/12953704) – adiga May 20 '19 at 13:23
  • 1
    You should not JSON.parse an object. – Christian Benseler May 20 '19 at 13:23
  • 1
    The key is "account.country".. you'll have to use bracket notation. `options['account.country']`. As for `JSON.parse`... that's for parsing JSON strings. If you want to turn a JS object into JSON use `JSON.stringify()` –  May 20 '19 at 13:23
  • 1
    JSON.parse(options) is not needed. options is allready object. If you wish to parse options must be string, options = '...' – MRsa May 20 '19 at 13:24

5 Answers5

0

You should use Bracket Notation when you want to access the property from a string.

const options = {
  "account.country": 'getCountry',
  "account.phone": 'getPhone',
}
console.log(options['account.country'])
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

When I console.log 'options', it shows the whole object

Because it is an object

when I console.log options.account it returns undefined

Because there is no account property.

Only account.country and account.phone. So you have to access properties with those explicit names, like this:

console.log(options['account.country']);

But it just returns 'Unexpected token o in JSON at position 1'

It's an object, not a string of JSON.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    This answer doesn't do anything to help the user understand the concepts he clearly doesn't. –  May 20 '19 at 13:26
  • 1
    @Jhawins — It clearly explains why the existing attempts fail, and demonstrates the modification needed to fix the problem. – Quentin May 20 '19 at 13:33
0
const options = {
  "account.country": 'getCountry',
  "account.phone": 'getPhone',
}

You can access the desired value using options['account.country']

James Coyle
  • 9,922
  • 1
  • 40
  • 48
Joseph
  • 682
  • 5
  • 17
0

let options = {
  "account.country": "getCountry",
  "account.phone": "getPhone"
}
console.log(options["account.country"])
Aks
  • 1,092
  • 3
  • 12
  • 29
0

The . in object key name is causing this issue. When you do:

options.account it returns undefined

^ that is because there is no key with name account

when I console log 'options.account.country' it errors

^ that is because it tries to look for key country on undefined (options.account)

You can solve this by using array indexing syntax as follow:

options['account.country']

options['account.phone']
Zain Zafar
  • 1,607
  • 4
  • 14
  • 23