This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have this code :
var dynamicVarName = id;
var percentage = res.parkeren[0].dynamicVarName.percentage;
DynamicvarName can have different values, but when i call this in the var percentage it's not working.
How can i get the dynamic variable in the array / object?
You need the bracket notation for the access:
var dynamicVarName = 'id';
var percentage = res.parkeren[0][dynamicVarName].percentage
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I just want to fetch object values by dynamic key, I need this because of web service providing data in this form
var apiData = [{first_name:"ashwani"},{second_name:"raju"},{third_name:"ravi"}]
var naming=["first","second","third"]
for(i=0;i<apiData.length;i++){
var symKey=naming[i] + "_name";
console.log(apiData[i].symKey)
}
But I am getting undefined, how can I do that ?
Use brackets [] instead of . notation.
.symkey will look for a property called symkey in the object.
var apiData = [{first_name:"ashwani"},{second_name:"raju"},{third_name:"ravi"}]
var naming=["first","second","third"]
for(i=0;i<apiData.length;i++){
var symKey=naming[i] + "_name";
console.log(apiData[i][symKey])
}
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
let property = $(this).val();
let property_name = this.dataset.property;
headObject[selected].property_name = property;
});
property_name is a variable not key. Are there anyway to handle this? Thank you in advance!
I can see you are uing jQuery.
headObject[selected].attr( property_name, property ); //Where headObject[selected] is a jQuery object
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 4 years ago.
CODE:
let name = 'bam';
doc.name = req.body[x].value;
PROBLEM:
I want it to return doc.bam value but actually it get doc.name value while I set name='bam'.
For computed properties you should use []
var doc = {};
let name = 'bam';
doc[name] = "abc";
console.log(doc.bam);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 4 years ago.
In the following code, I am trying to set backgroundColor via a variable prop. I would like to know the reason as to why div.style.prop does not work.
var div = document.getElementById('test')
var prop = 'backgroundColor'
div.innerHTML = 'This is some text'
div.style.prop = 'red'
This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 8 years ago.
var diena = "example"
array.push({diena: sub_array});
I want to array key use variable and have key "example" not "diena". How I can do this ?
You'll need to construct the object beforehand.
var diena = "example";
var obj = {};
obj[diena] = sub_array;
array.push(obj);