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
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:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 2 years ago.
Say I have an javascript object
i = {
data1:{
one:'555',
two:'222'
},
data2:{
}
}
I am suprised to see that there is no way to say i{data1}{one} to arrive at answer 555.. What is the most current way to get at this data without using a for loop?
You can easily arrive there with:
i.data1.one
or
i['data1']['one']
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
I want to add a variable to my code, so it calls different functions when I need it but it doesn't work because of the string's quotes. What type should I use or how to strip those quotes from the string?
You could use brackets accessor :
rawData(){
return this.$store.getters['get'+this.dataName]
}
rawData should be a computed property and 'get'+this.dataName has to be in your getters like getTodos
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access object properties containing special characters?
(2 answers)
Closed 5 years ago.
I've got some data that's getting returned from an API and converted into a js object in PHP before being passed back to the browser. One of the values i'm trying to retrieve from this object lives in the objects attributes. Here is what the object structure looks like in JS:
Currently I can access all properties I need to by calling object.comments for example or object.email.whatever.
What I can't do is access the objects attributes. Ideally I would like to get to the ID via something like object.#attributes.id but this returns an error.
Is it possible to access an objects attributes and if so how should I go about it?
Thanks
To access properties using dot notation the property must be a valid identifier. If it's not, you have to use brackets:
object['#attributes'].id
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.