简体   繁体   中英

Get data from object where value name has a space

For example, I can get this to return the right city with my data:

console.log(selected_things[0].City);

However, I need to actually get the zip code, but this doesn't work.

console.log(selected_things[0].Zip Code);

How do you handle this where the value name has a space in it?

use the square bracket notation again with a string literal

console.log(selected_things[0]["Zip Code"]);

All properties of an object can be accessed by the dot notation followed by the name or the square bracket notation with a string as the indexer.

The caveat is when the attribute name is an invalid identifier and therefore must be accessed with square bracket notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM