Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  45] [ 1]  / answers: 1 / hits: 141242  / 13 Years ago, tue, november 29, 2011, 12:00:00

I have a JavaScript object that looks something like this:



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


I can access the properties of cartoon easily using myTextOptions.cartoon.comic or whatever. However, I haven't been able to get the syntax right for accessing kid. 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
myTextOptions.character%20names.kid

More From » object

 Answers
6

Use ECMAscripts bracket notation:



myTextOptions[ 'character names' ].kid;


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



For more information read out here:




[#88841] Monday, November 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reed

Total Points: 725
Total Questions: 85
Total Answers: 89

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;