Updating object values in JavaScript via function [duplicate] - javascript

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;
}

Related

Value of function parameter is not getting [duplicate]

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(...)})

How to dynamically set a property in jquery? [duplicate]

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.

Do Javascript variable assignments work by reference? [duplicate]

This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 8 years ago.
I have a question about referencing objects in javascript.
Say I have a variable that is some object (lets say json) and it is called objOne - (var objOne = someJSONObject;).
If I go ahead and declare
var objTwo = objOne;
will I have two references to the same Object? Kind of like a c pointer?
To sum it up :
assignements are done by value
you never manipulate objects, only object references
This means that
you'll have two references to the same object (you can check that by changing a property of the object)
when you're passed in a variable the value of a primitive, changing your variable doesn't change other variables
EDIT : as it's a duplicate I'll delete this answer in a minute to allow a proper closing if there's no other answer. Please vote to close.
Yes, objects are passed by reference:
function changeVal(obj){
obj.value = "bar"
}
(function checkRefs(){
var myObject = {
value: "foo"
};
alert(myObject.value);
changeVal(myObject);
alert(myObject.value);
})();

Get the name property of a variable [duplicate]

This question already has answers here:
Get variable name. javascript "reflection"
(4 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 8 years ago.
I was wondering if it's possible to get the name of a variables in javascript or JQuery.
Suppose that I declare a variable in javascript like:
var customerNr = "456910";
If a function receive a variable, how can i return the name of the variable?
For example:
function getNameOfVariable(someVariable){
//return name of someVariable;
}
If I call this function like this:
getNameOfVariable(customerNr);
The function has to return a string whose value is "customerNr".
How can I do this in jquery or javascript? Some kind of reflection?
That is simply not possible!
The passed parameter doesn't even have to have a name. It could be a return value of a function or a raw literal (like "string" or 3).
No, this is not possible. Only values are transferred for primitive data types, not references, so how would you know what the "name" of the variable is? For example:
var customerNr="456910";
var customerNrAfterProcessing=customerNr;
getNameOfVariable(customerNrAfterProcessing);
Would it return customerNrAfterProcessing or customerNr?
However, you can imitate this behavior with objects.
var variableName = someMethodThatDeterminesVariableNameOrJustAConstantIfYouPrefer();
var myVariables={};
var myVariables[variableName]={};
myVariables[variableName].value="456910";
myVariables[variableName].variableName=variableName;
function getNameOfVariable(someVariable){
return someVariable.variableName;
}
getNameOfVariable(myVariables[variableName]);
Obviously, this amounts to a lot more work than just using the variableName variable directly, but it could be necessary in some more complicated situations.
See working with objects in JS reference

Javascript get all Properties of Object [duplicate]

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]

Categories

Resources