How can I access a JavaScript object which has spaces in the object's key?
Use ECMAscripts "bracket notation":
myTextOptions[ 'character names' ].kid;
You can use that notation either way, reading & writting.
For more information read out here:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Do Javascript objects allow keys that contain spaces?
Yes, a propery name can be any string (all values are coerced to strings before they are used internally).
This can be done using quotes (double or single) around names in literals:
{ Applications: 1, "Computing Servers": 0, .. },
Just as it can be done with using the obj[prop]
syntax (where prop
is any expression that can be converted to a sensible string value):
obj["Computing Servers"] = 42;
how to use a object key with - in the middle
While defining the array media-metadata
inside obj
it should be wrapped in ""
and when mapping over it then you need to use []
like
const obj = {
approved_for_syndication: 1,
caption: "",
copyright: "",
"media-metadata": [{
name: "test1"
}, {
name: "test2"
}, {
name: "test3"
}],
subtype: "photo",
};
const result = obj["media-metadata"].map((o) => o.name);
console.log(result);
How to access json object key with space
Change
console.log(data[key].Per Mertesacker);
to
console.log(data[key]['Per Mertesacker']);
Read Working with objects
Accessing JSON object keys having spaces
The way to do this is via the bracket notation.
var test = { "id": "109", "No. of interfaces": "4"}alert(test["No. of interfaces"]);
How to access Object parameters having spaces in javascript
Spaces in attribute names are perfectly OK in JavaScript. You reference them by the
object["param name"]
syntax. Moreover, any UTF8 character, except for control characters, are valid in attribute names.
Accessing object key with dot notation and space
Why is this valid?
Because whitespace is largely (though not entirely) irrelevant in the JavaScript syntax. You can safely insert any whitespace other than a line break between two tokens (and in most, but not all cases, you can insert line breaks as well; the "most" is due to ASI). You can't insert spaces within tokens (because it breaks them up into two tokens), but you can between tokens.
As Federico klez Culloca points out (link), .
is an operator, just like +
or *
. The fact we generally don't put spaces around it, but do put spaces around them, is simply convention.
These are all valid:
console.log(object.name);
console.log(
object.name
);
console.log(
object . name
);
console.log(
object
.
name
);
Is there any ECMA specification for this?
Of course, the specification itself. Specifically here and here. From that last link:
Input elements other than white space and comments form the terminal symbols for the syntactic grammar for ECMAScript and are called ECMAScript tokens. These tokens are the reserved words, identifiers, literals, and punctuators of the ECMAScript language. Moreover, line terminators, although not considered to be tokens, also become part of the stream of input elements and guide the process of automatic semicolon insertion (11.9). Simple white space and single-line comments are discarded and do not appear in the stream of input elements for the syntactic grammar.
Related Topics
Insert HTML at Caret in a Contenteditable Div
Why Does JavaScript Handle the Plus and Minus Operators Between Strings and Numbers Differently
What Is the Meaning of the 'G' Flag in Regular Expressions
How to Automate Shadow Dom Elements Using Selenium
Filter Object Properties by Key in Es6
Declaring Variables Without Var Keyword
Angularjs "Controller As" Syntax - Clarification
Does IE9 Support Console.Log, and Is It a Real Function
How to Get Character Array from a String
JavaScript Document.Getelementsbyclassname Compatibility with Ie
Object Property Name as Number
What Do These JavaScript Bitwise Operators Do