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]
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I'm not getting the actual value of my parameter name in the below function. Object.assign taking the function parameter name as a string literal so the resulted JSON object also named as name
see the below code.
Please see the resulted json object i got.
How to fix this?
wrap it with []
return Object.assign(..., {[name]: JSON.parse(...)})
This question already has answers here:
Adding elements to object
(19 answers)
Closed 6 years ago.
I would like to know how to add/ remove properties to object at run time in javascript? How to achieve this in javascript ?
Let's say your object is myobj
then you can add a member like this
myobj.myvar = value; or myobj["myvar"] = value;
and remove it with
delete myobj.myvar; or delete myobj["myvar"];
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.
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:
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