This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 6 years ago.
I have an object that stores 3 variables called text0, text1 and text2 that in turn store one string each. I also have a variable that stores the index of the clicked button, so that if I click on the third button it should fetch the text2.
I was trying something like this:
p.text(myObj.text + btnNumber);
But so far all I get is NaN. Is it even possible to do this way? What am I missing?
Use named indexing of the object to get a variable named property:
p.text(myObj["text" + btnNumber]);
Javascript lets you index properties of an object as if it were a dictionary (indexed via string names).
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
Using JS in conjunction with a JSON file to turn a room ID into the room information.
function rooms(roomID) {
const roomsDB = require("./rooms.json")
let roomsID = "r".concat("", roomID).toString()
console.log(roomsDB.roomsID)
}
rooms(46)
This is the function I'm using to turn the ID into the info, and the rooms.json file is as follows:
{
"r46":"house"
}
Ideally, this would log house into the console, but I only get undefined.
Why isn't the JSON object being called properly?
To dynamically access an object property by name in a string variable you need to use brackets:
roomsDB[roomsID]
The dot (roomsDB.roomsID) is just like roomsDB["roomsID"], it doesn't substitute the variable.
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
As the title says, I'm trying to use a var inside of a form name.
function validate_inventory_form(location){
var name=document.inventory_+[location]+_input.name.value;
...
}
What would the correct syntax be to add the location to the form name so the var name would equal the result of document.inventory_cupboard_input.name.value for example?
Thanks in advance.
You can use bracket notation to access the property, this allow you to use a custom built string as the key.
Something like document["inv" + loc + "input]
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:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
dynamically select a javascript object attribute
(1 answer)
Closed 8 years ago.
I have a javascript object (named resource) used for translating my texts.
resource
.fileNotFound >> "File not found"
.advSearch >> "Advanced search"
Usually I use this like below:
alert (resource.advSearch);
Now I need to access one of the member of this resource object through a variable.
Example:
var trans = "advSearch";
My question: how can I get the translation in my resource object for 'advSearch' contained in the trans variable?
Thanks.
You need to use the bracket notation instead of dot notation as the member operator
resource[trans]
You can use the [] notation to access properties as well.
resource[trans];
This question already has answers here:
Javascript create variable from its name
(5 answers)
Closed 8 years ago.
I need to run this line of Javascript:
#model.save({ name: #some_element.val() })
But the key, which in this case is name, will change depending on the value of a variable. The variable is a string representation of the key. So in this case, the variable is "name". How can I use the variable to specify the correct key? If I use the variable name directly it is interpreted as the key itself.
var obj = {};
obj[varName] = #some_element.val();
#model.save(obj);