This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 5 years ago.
I am getting several arrays returned from a GET reponse, which I need to access. Since their names can change based on the request, I have a variable specifing which array to use.
However I always get undefined. See this:
console.log(current); // trips_out_201702
console.log(theResponse.current); // undefined
console.log(theResponse.trips_out_201702); // this works
How can I make theResponse.current work such that it returns what current actually stands for? Why do I get undefined there?
When the property key in an object is a variable you can use the square bracket notation to access that property.
console.log(theResponse[current]);
when acessing with dynamic attribute You should do as
theResponse[current] not theResponse.current
You are trying to get array value using object's way.
You should try this one instant variable['keyName']
Good Luck!
Related
This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 1 year ago.
I wrote a utility function for my nextjs project. In there, I got something that I unexpected. I initialised an empty array which be filled with the data later. When I type check that empty array, it shows an object. I don't want an object. I want an array. Why? Could someone told me why does it happen.
strong text
Yes typeof array is an object in javascript
if you want to check one variable is an object or array then you can use
Array.isArray()
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(obj)) // false
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
So this is a bit of an unusual situation i am in but what i need to do is access the value of an object based on what index its stored in. The problem is that i need it in my Select component of material ui. So the overview is that i store alginment values of a video . the videos show up in a loop which means the select values are repeated and to know if its for the first video i append the index inside the object like this:
Now in order to set the option i have to access this value here :
Now the problem is in this loop i cant do something like :
halign.halign[index] because obviously that would mean its an array. So long story short how could i do something like :
value={`${halign.halign}${index}`}
So basically the end result for value to evaluate is : halign.halign0
and so on for each index.
NOTE the outer halign is the main useState object.
Remember that for JavaScript objects x['y'] and x.y are interchangeable.
So if you need to compute the key you're looking up, use:
halign['halign' + index]
Or template strings if you prefer.
Note this would be a lot easier if you organized your object with an internal array, so you could just do halign[index].
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:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have an object with some data inside. The first level of data are 2 arrays (body, cause). Each body and cause array have arrays inside of them (date, year).
totals:[{body:[
{date:[54,9,3,17]},
{year:[437,61,31,140]}]},
{cause:[
{date:[54,9,3,17]},
{year:[437,61,31,140]}]
}]
What I would like to do is access the body/cause array dynamically based on something the user has changed.
This is how I am accessing them now.
totals[isCause].body[isYear].date[filterNumber]);
My issues is body and date are hard coded in there, and I would like to have access to either body/cause date/year. I can't seem to find what these property names are stored as. I tried to set up a var and do something like this
var bodyCause = "body";
Then I tried to pass it back into my retriever statement.
totals[isCause].bodyCause[isYear].date[filterNumber]);
But that fails. So I'm just trying to figure out what that property name is stored as and if I can dynamically set it when I need to retrieve information.
Your attempt was almost correct. You can easyly use var bodyCause = "body"; and access the content dynamically. Instead of this
totals[isCause].bodyCause[isYear].date[filterNumber]);
you should use this
totals[isCause][bodyCause][isYear].date[filterNumber]);
Should fix your problem.
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I have an object and a few variables I'd like nested inside the variable but for some reason I can only ever get the first nest. Anything after that gives me an error stating that it couldn't read because it was undefined:
var date = 10;
var timestamp = 100;
Que[date] = {timestamp:{"test":"test"}};//this returns {10:{"timestamp":{"test":"test"}}}; for some reason
Que[date][timestamp] = {"test":"test"}; //errors saying, cannot read '100' undefined
console.log(Que);
I'm not sure why this is happening and I'd really like to resolve it with simple means. Btw the Que variable is global inside another script and predeterminietly contains {"10":{"24":{"1":"test"}}}; which is likely why the date variable does work but the timestamp variable doesn't. Any suggestions?
EDIT:
date, and timestamp are both declared outside of the object, however i wish to use them as key's inside of the object..
uppon reading the mod suggested post and implementing what it contained, i ended up with another error
Que = {[date]:{[timestamp]:{"test":"test"}}};//this results in an unexpectd token error located at the [ before date
I think there are more questions to be asked based off of your code snippet, but in your example timesheet is not defined, you can reference the key inside your JSON by using
Que[date]["timesheet"]
To access the object inside. The key that you're looking up with the [] notation should be a string.