This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access a JavaScript object which has spaces in the object's key?
(4 answers)
Closed 10 years ago.
Suppose if a object has parameters var data={"f name":"vishal"} and i want to access parameter "f name" in javascript. I know that there should not be space in parameters of a object but if it has space in it then how can i read it.
Something like this:
data['f name']
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.
Use square bracket notation:
data["f name"]
Related
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
I got a json and it has special characters inside.
myJSON = "metadata": Object {
"album-art": "album-art.png",
}
However - Variable declaration is not possible due to the special character "-".
const Variable = myJSON.album-art
Can I put it inside a variable?
It's a better practice to use _ instead of -,
but you can access the variable using myJSON["prop"]
myJSON = {
"album-art": "album-art.png",
}
console.log(myJSON["album-art"])
Do this.
There are two methods to access a property/method in a object. One is dot notation and the other is square bracket notation (example below)
const Variable = myJSON['album-art']
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 3 months ago.
I am unable to retrieve a value from a json object when the string has a dash character:
{
"profile-id":1234, "user_id":6789
}
If I try to reference the parsed jsonObj.profile-id it returns ReferenceError: "id" is not defined but jsonObj.user_id will return 6789
I don't have a way to modify the values being returned by the external api call and trying to parse the returned string in order to remove dashes will ruin urls, etc., that are passed as well. Help?
jsonObj.profile-id is a subtraction expression (i.e. jsonObj.profile - id).
To access a key that contains characters that cannot appear in an identifier, use brackets:
jsonObj["profile-id"]
In addition to this answer, note that in Node.js if you access JSON with the array syntax [] all nested JSON keys should follow that syntax
This is the wrong way
json.first.second.third['comment']
and will will give you the 'undefined' error.
This is the correct way
json['first']['second']['third']['comment']
For ansible, and using hyphen, this worked for me:
- name: free-ud-ssd-space-in-percent
debug:
var: clusterInfo.json.content["free-ud-ssd-space-in-percent"]
For anyone trying to apply the accepted solution to HomeAssistant value templates, you must use single quotes if you are nesting in doubles:
value_template: "{{ value_json['internet-computer'].usd }}"
If you are in Linux, try using the following template to print JSON value which contains dashes '-'
jq '.["value-with-dash"]'
It worked for me.
This question already has answers here:
Template String As Object Property Name
(3 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
How do I use template literal as key inside object literal?
The following code:
{
`${id}_name`: "John Doe",
`${id}_age`: 27,
}
Produces a syntax error:
Uncaught SyntaxError: Unexpected token :
Is there something I'm missing, or is this not supported? If not, is support for this feature planned?
And of course I come up with a solution straight away:
{
[`${id}_name`]: "John Doe",
[`${id}_age`]: 27,
}
Wrap the template literal in square brackets. This will evaluate the expression in between the square brackets and use it as the key.
Without square brackets, only identifiers (e.g. my_key_name), single or double quote strings (e.g. "My Key Name") and numbers (e.g. 42) can be used.
Your answer is correct, but i will try to answer the why of it.
If we refer to the Ecmascript object definition, it is said
Properties are identified using key values. A property key value is either an ECMAScript String value or a Symbol value. All String and Symbol values, including the empty String, are valid as property keys. A property name is a property key that is a String value.
In this case, a ECMAScript string is defined as
A string literal is zero or more Unicode code points enclosed in single or double quotes. Unicode code points may also be represented by an escape sequence. All code points may appear literally in a string literal except for the closing quote code points, U+005C (REVERSE SOLIDUS), U+000D (CARRIAGE RETURN), and U+000A (LINE FEED).
Which mean a string has to be unicode caracter enclosed in single or double quotes. And Object keys must be ECMAScript string or Symbol, which i assume are index.
A template literal is a ECMAScript string but an ECMAScript string is not a template literal.
P.S. Take this with a grain of salt, i'm not an ECMAScript expert but that's what i could find.
This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 4 years ago.
I have some code which is giving me the following error when I rename a class name.
This gives no error:
this.container = document.createElement("ul"),
But when I rename the container class name to this:
this.tt-container = document.createElement("ul"),
I get the following error:
Syntax error: Invalid left-hand side in assignment expression
How can I fix this?
use _ instead of -, so this.tt_container, not this.tt-container
From MDN,
An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation.
In your case - is not a valid JavaScript identifier. To use -, Use Bracket Notation
this["tt-container"] = document.createElement("ul"),
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 3 months ago.
I am unable to retrieve a value from a json object when the string has a dash character:
{
"profile-id":1234, "user_id":6789
}
If I try to reference the parsed jsonObj.profile-id it returns ReferenceError: "id" is not defined but jsonObj.user_id will return 6789
I don't have a way to modify the values being returned by the external api call and trying to parse the returned string in order to remove dashes will ruin urls, etc., that are passed as well. Help?
jsonObj.profile-id is a subtraction expression (i.e. jsonObj.profile - id).
To access a key that contains characters that cannot appear in an identifier, use brackets:
jsonObj["profile-id"]
In addition to this answer, note that in Node.js if you access JSON with the array syntax [] all nested JSON keys should follow that syntax
This is the wrong way
json.first.second.third['comment']
and will will give you the 'undefined' error.
This is the correct way
json['first']['second']['third']['comment']
For ansible, and using hyphen, this worked for me:
- name: free-ud-ssd-space-in-percent
debug:
var: clusterInfo.json.content["free-ud-ssd-space-in-percent"]
For anyone trying to apply the accepted solution to HomeAssistant value templates, you must use single quotes if you are nesting in doubles:
value_template: "{{ value_json['internet-computer'].usd }}"
If you are in Linux, try using the following template to print JSON value which contains dashes '-'
jq '.["value-with-dash"]'
It worked for me.