This question already has answers here:
Template literals like 'some ${string}' or "some ${string}" are not working
(7 answers)
Closed 1 year ago.
I have a variable saved as object_id in javascript.
I want to target a data attribute by interpolating the object id in to the data attribute.
For example:
$('[data-object-id="${object_id}"]')
However this is not finding the attribute. The are no errors.
I've tried interpolating using '+' but found it quite fiddly in this instance.
For interpolation in string you need to use backticks(`) string. Try:
$(`[data-object-id="${object_id}"]`)
Related
This question already has answers here:
How can I access a JavaScript object which has spaces in the object's key?
(4 answers)
Selecting a JSON object with a colon in the key
(1 answer)
How can I access object properties containing special characters?
(2 answers)
Closed 1 year ago.
I am building a site using javascript.
My data comes from a FileMaker API.
My goal is to fill an array with the data retrieved from the FileMaker API.
Except that this data comes from a linked table in FileMaker, which appends :: to the name of the variable.
Here is the code I'm trying to do, but with :: it doesn't work. How can I go about including ::?
for(var i=0;i<allFEC.length;i++)
{
programmesMovies.push({
"isOnline_b":1,
"_idWeb_c": allFEC[i].pro_FIP::_id_FEC
});
}
Anything that has spaces or any such weird syntax you can access it using square brackets
for(var i=0;i<allFEC.length;i++)
{
programmesMovies.push({
"isOnline_b":1,
"_idWeb_c": allFEC[i]["pro_FIP::_id_FEC"]
});
}
It works with spaces too. If you have pro_FIP _id_FEC as a property the same syntax will work. i.e allFEC[i]["pro_FIP _id_FEC"]
This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
I have following simple javascript code:
s="hello"
s[0]="X"
console.log(s[0])
The output is h, but I have updated it as X, I would ask why I got such result, thanks.
Strings in JavaScript are primitives, which means they are immutable, ie. you are not able to change them. If you were running your code in strict mode then this would throw an error.
If you wanted to modify this string you would need to effectively create a copy of it, and then assign that to the s variable eg.
s = "X" + s.substring(1)
This question already has answers here:
Square Brackets Javascript Object Key
(5 answers)
Closed 2 years ago.
I have API data in my React component. It's in the form of
{weather.Wind.Speed.Metric.Value}
Is there any way I can swap Metric out for a variable such as unit.
For example something like
const unit = 'Metric';
{weather.Wind.Speed.${unit}.Value}
That way I can update the variable and show the correct data?
You can use the bracket notation: weather.Wind.Speed[unit].Value.
More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 6 years ago.
Ok. The code here is in fact Javascript but I can not find a way to fixing the problem in javascript. The code below is javascript.
var =inputdata;
dataout = data.items.inputdata.time
This is how it would sort of look like if it was php
dataout = data.items.$inputdata.time
I would like inputdata to be treated as a variable and not as text.
Sorry for the small amount detail
You can use square bracket notation:
dataout = data.items[inputdata].time
This will allow you to use a string in place of a key for a javascript object.
This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?