This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I am using toastr and want to set the options with a json object that is returned from ajax call. I am having some trouble setting the options property and value dynamically. Here is sample code:
if(data.toast){
$.each(data.toast, function(index, element) {
toastr.options.index = element;
});
}
And data.toast is a json object that looks like this:
"toast":{"closeButton":true}
Here is what it would look like if I hardcoded it:
toastr.options.closeButton = true;
What should I change the .index in the iterator to so that it will evaluate it as a variable
if(data.toast){
$.each(data.toast, function(index, element) {
toastr.options[index] = element;
});
}
Doing it as options.index attempts to access the property of index not the property of the value of index.
The above method will fix that. It treats the object as a sort of associative array. So toastr.options[index] evaluates to toastr.options["closeButton"] which is the same as toastr.options.closeButton.
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have a popup form and need to return a value to the master form. The first statement works fine, but when I try to put the field name (textAlert) to a variable, I cannot get it to work. I tried all kinds of syntax.
Please help. Thanks.
function DoReturnValue()
{
opener.MasterForm.textAlert.value = "Hello"; // WORKS
/* DOES NOT WORK
var theField = "textAlert";
opener.MasterForm(theField).value = "Haloha";
opener.document.MasterForm.getElementById(theField).value = "Haloha";
*/
}
To access properties dynamically with a string variable as a name, use square-bracket notation:
opener.MasterForm[theField].value = "Haloha";
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 9 years ago.
In JavaScript I am trying to update an object value through a function, through which I am passing the object property to update.
However, this won't work - and I can see why, but don't know how to combat it!
myObject = {"testItem": "testValue"};
console.log(myObject.testItem);
function updateSomeValue(objectItem, newValue){
myObject.objectItem = newValue;
}
updateSomeValue('testItem', 'newValue');
console.log(myObject.testItem);
Now, I can see the issue here is that in the function, myObject.objectItem is expecting an item in the object called objectItem - it won't translate it to testItem.
How do I do this?
By using a different notation. Using [ .. ] you can specify the property name as a string.
function updateSomeValue(objectItem, newValue){
myObject[objectItem] = newValue;
}
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
How to get function parameter names/values dynamically?
(34 answers)
Closed 9 years ago.
I have an Javascript Object and I want to give out all Properties of this Object.
Currently I´m having this Piece of Code, which gives me the Name of all Properties.
But if it´s a Function I also need all Parameters the Function would accept.
for(var property in Object) {
console.log(property);
}
Output
...
...
...
TD
explicitJoin
hashCode
getED
queryConditions
getDisplayTagName
getClass
displayValue
addCondition
getEncodedString
getDisplayValue
addOrCondition
multiple
...
...
...
By using
for(var property in Object) {
console.log(property);
}
you will get key of each element, If that property is a function and you want to use values than use
Object[property]
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 10 years ago.
I have a javascript issue.
If I have an object array objAr, the object consists of id,name.
If I was to access objAr[0].id it returns the id value of the first object. What would happen if the object is dynamic and therefore I do not know what it consists of, is there a way to dynamically call the Object attribute?
Currently I am creating another array
var theArr = new Array("id", "name");
and call:
objAr[0].theArr[0] instead of objAr[0].id.
Is there a way to do this better using Javascript?
With Javascript you can call all of the attributes in an object without knowing the keys.
See below:
for(key in objAr[0]) {
console.log(objAr[0][key]);
}
If you just wanted the first attribute you could run:
for(key in objAr[0]) {
var attFirst = objAr[0][key];
break;
}
Additionally for the JS array you could have used square brackets.
var theArr = ["id", "name"];
hope that helps
In javascript you can always use the "array notation" in place of the "dot notation"
So these 2 lines are the same
objAr[0].id
objAr[0]["id"]
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
How to access an object using a variable as key. Here is my code sample:
var o = {"k1": "111", "k2": "222"};
alert(o.k1); //working fine
var key = "k"+1; alert(key); // k1
alert(o.key); //not working
You can access objects like arrays:
alert(o[key]);
Change the last line to: alert(o['k1']); or alert(o[key]); where key is your dynamically constructed property key.
Remember you can access object's properties with array notation.
Consider using a for...in loop