Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access array key with space

How can i access an array key with a space?

Standard way,

{{MyArray.name}}

Key with space,

{{MyArray.first name}}  or {{MyArray['first name']}}

doesn't work.

like image 606
shapeshifter Avatar asked Jan 11 '13 00:01

shapeshifter


People also ask

How do I access a key with space?

Use bracket notation to access a key that contains a space in an object, e.g. obj['my key'] . The bracket [] notation syntax allows us to access an object's key, even if it contains spaces, hyphens or special characters.

Are spaces allowed in JSON keys?

Whitespace (Space, Horizontal tab, Line feed or New line or Carriage return) does not matter in JSON. It can also be minified with no affect to the data.

Can object Key have space?

Object Keys Are Strings. Object keys with underscores and camelCase (no spaces) don't require the bracket syntax, but object keys with spaces and hyphens do. You may prefer the code readability of the . dot syntax, in which case you'll want to avoid spaces in your object keys: you might write object.

Can object property have space?

JavaScript object property names can be any string, including having spaces in the name. However, object property names that are not valid JavaScript identifiers, can only be: Specified using quotes, and; Accessed using the bracket property accessor notation.


1 Answers

Use brackets in conjunction with dots, without quotes:

{{MyArray.[first name]}}

like image 54
Brad Urani Avatar answered Oct 12 '22 23:10

Brad Urani