Home > Article > Web Front-end > How do you access JavaScript object properties with spaces in their keys?
Accessing JavaScript Objects with Spaced Keys
When declaring JavaScript objects, key names can contain spaces, but accessing them using the dot notation (e.g., myObject.child) can be challenging.
Problem:
Accessing properties of JavaScript objects with keys containing spaces, such as "character names" in the given object:
var myTextOptions = { 'cartoon': { comic: 'Calvin & Hobbes', published: '1993' }, 'character names': { kid: 'Calvin', tiger: 'Hobbes' } };
Solution:
To access such properties, use ECMAScript's "bracket notation":
myTextOptions[ 'character names' ].kid;
The bracket notation allows you to use strings or expressions to access properties, resolving space issues. It works for both reading and writing object properties.
Additional Information:
The above is the detailed content of How do you access JavaScript object properties with spaces in their keys?. For more information, please follow other related articles on the PHP Chinese website!