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']
Related
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:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am unable to get the data of an object
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a.val[0])
}
fun('ab','ef')
It should output 'cd' but it is giving out error in the console
any idea how do i fix this...
Use bracket notation like so:
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a[val[0]])
}
fun('ab','ef')
Your code was trying to get the property named val in a (doesn't exist), then get the first character/item of that value (trying to do this to undefined causes the error).
This question already has answers here:
jQuery sort array value in sequence
(1 answer)
Sorting javascript array using function 'sort"
(3 answers)
Closed 5 years ago.
Just wondering, what is the best way to go about "sorting" an array to the following:
I have the array as [1,1,1,1,2,2,2,2], how would I sort it so it goes:
[1,2,1,2,1,2,1,2]
I cannot think of the phrase or a method to do this.
This is done in angularjs/can use jquery.
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:
How to get objects value if its name contains dots?
(4 answers)
How can I access object properties containing special characters?
(2 answers)
Closed 9 years ago.
I have a property with name 'pp.phaseName' in an object 'Config'
Whenever I try to access it like Config.pp.phaseName, it's throwing error.
I've tried using Config.(pp.phaseName), Config."pp.phaseName" etc. but none was working.
Please help me on how to do this.
You have to use the square bracket notation.
Config["pp.phaseName"]