Home > Article > Web Front-end > How to Access JavaScript Objects with Spaces in Property Names?
Accessing JavaScript Objects with Spaces in Property Names
When working with JavaScript objects, you may encounter instances where the property keys contain spaces. Attempting to access these properties using dot notation (e.g., object.property name) will not work.
Consider the example:
var myTextOptions = { 'cartoon': { comic: 'Calvin & Hobbes', published: '1993' }, 'character names': { kid: 'Calvin', tiger: 'Hobbes' } };
To access the kid property, we cannot use myTextOptions.character names.kid. Instead, you need to employ ECMAscript's "bracket notation":
myTextOptions[ 'character names' ].kid;
Bracket notation allows you to specify any valid JavaScript expression within square brackets, including property names with spaces.
This notation can also be used for assigning values:
myTextOptions[ 'character names' ].newProperty = 'value';
For more information on working with objects in JavaScript, refer to the following resource:
The above is the detailed content of How to Access JavaScript Objects with Spaces in Property Names?. For more information, please follow other related articles on the PHP Chinese website!