julio julio - 1 year ago 89
Javascript Question

access javascript object with space in key

I have a javascript object that looks something like this:

myTextOptions: {
'cartoon': {
comic: 'Calvin & Hobbes',
published: '1993'
'character names': {
kid: 'Calvin',
tiger: 'Hobbes'

I can access the cartoon properties easily using
or whatever. However, I haven't been able to get the syntax right for accessing
. I've tried the following with no luck:

myTextOptions.character names.kid
myTextOptions."character names".kid
myTextOptions.character\ names.kid
myTextOptions.'character names'.kid
myTextOptions.["character names"].kid

Answer Source

Use ECMAscripts "bracket notation":

myTextOptions[ 'character names' ].kid;

You can use that notation either way, reading & writting.

For more information read out here: