Babel string interpolation error [duplicate] - javascript

This question already has answers here:
Babel error: JSX value should be either an expression or a quoted JSX text
(3 answers)
Closed 6 years ago.
I have a bunch of input elements to which I want to assign unique ids.
However, the following string interpolation (i is the mapped element index)
id = `input-add-${i}`
returns
Parsing error: JSX value should be either an expression or a quoted JSX text
I use interpolation successfully elsewhere, so I don't understand what I am doing wrong here.

Ah, my bad. I forgot the curly brackets.
This works
{ id={`input-add-${i}`} }
and this would also work
id={'input-add-' + i}

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.

Match text in nested parenthesis [duplicate]

This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
How to match a input parenthesis with regular expression in JavaScript?
(4 answers)
Closed 4 years ago.
As the title says, I cant figure out how to match text inside a nested parenthesis (only in nested parenthesis). E.g
I have this text Hi my (name is (Jhon) and(i dont) know how (to)match) this.
The text I need to match is Jhon, i dont and to.
But text like this one hi i have (a parenthesis) should not match anything
Every answer i've seen until now uses recursive regex but I can not use a recursive regex in js and also I dont need a dinamic nested level identifier, just the text (whatever text) is inside the second parenthesis. ((this text))
I've tried (with no success) this {({([^{}])*})*}
This is not a dupe since The guy at the other question just ask for a single parenthesis....

Javascript Invalid left-hand side in assignment expression when renaming [duplicate]

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"),

How to get rid of everything inside <> brackets in Javascript? [duplicate]

This question already has answers here:
Strip HTML from Text JavaScript
(44 answers)
Closed 4 years ago.
I have a text that looks like
<00:02:00.820><c> ему</c><00:02:00.970><c> кто</c></c><c.colorE5E5E5><00:02:01.180><c> пойдет</c></c>
I need to get rid of everything that is inside the <> tags, so the result looks like:
ему кто пойдёт
How would I do that using Javascript? Would regex like this work:
string.replace([<^\>]*],'');
You need to pass either a string or a regular expression literal into .replace's first argument; .replace([<^\>]*], isn't valid syntax. I'd prefer to lazy-repeat any character until getting to another >, with <.*?>:
const str = '<00:02:00.820><c> ему</c><00:02:00.970><c> кто</c></c><c.colorE5E5E5><00:02:01.180><c> пойдет</c></c>';
console.log(
str.replace(/<.*?>/g, '')
);

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.

Categories