Put special characters inside JavaScript variables [duplicate] - javascript

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']

Related

how to access json field with "-" e.g. 'data-id': 'smth' [duplicate]

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.

How to give input to replace() when the string is present in an array [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 3 years ago.
I have to replace the special characters in my string with underscore. replace() function is used to do it . I know that.
My string is "545123_Claims#Claims#Claims000117".
But the issue is that replace() accepts sting as input.
Actually my string is in an array like filArr= ["545123_Claims#Claims#Claims000117"].
So how can I replace the special character in thisstring which is inside an array?
You could map the replaced strings by taking a function.
const replacementFn = string => string.replace(/xxx/, 'yyy');
filArr = fillArr.map(replacementFn);

How to get "#" from JSON [duplicate]

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.

Access Javascript Object Key with special symbol [duplicate]

This question already has answers here:
Unable to access JSON property with "-" dash [duplicate]
(5 answers)
Closed 5 years ago.
var obj = {
a?: "abc"
}
Want to access Key Which is Not in a valid format.
Giving me Error.
I'm accessing it like this obj.a.
and this is throwing an error.
const obj = { a?: 'aaa' }
console.log(obj.a)
This could work (not tested)
obj['a?'];
Valid property names
Looking at the ECMAScript spec grammar, we can see that a property
name can be either an identifier name (i.e. identifiers + reserved
words), a string literal, or a numeric literal.
Identifier names are a superset of identifiers; any valid identifier
and any reserved word is a valid identifier name.
[...]
When can the quotes be omitted?
Unless an object key is a numeric literal or a valid identifier name,
you need to quote it to avoid a syntax error from being thrown. In
other words, quotes can only be omitted if the property name is a
numeric literal or a valid identifier name. Of course, if the property
name is a string literal, it’s already quoted by definition
source: https://mathiasbynens.be/notes/javascript-properties
To solve your specific issue you can use quotes:
var obj = {
'a?': "abc"
}
obj['aj']
Full answer can be found at
https://stackoverflow.com/a/9571440/1211174
The key should be a simple string in JavaScript, so if you change from a? to a, it should work.

How to access Object parameters having spaces in javascript [duplicate]

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"]

Categories