Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating JavaScript object-attributes from another object [duplicate]

I want to update an object that could look like this:

currentObject = {     someValue : "value",     myObject : {         attribute1 : "foo",         attribute2 : "bar"     } }; 

.. with an object that contains some changes i.e.:

updateObject = {     myObject : {         attribute2 : "hello world"     } }; 

At the end I would like to have currentObject updated so that:

currentObject.myObject.attribute2 == "hello world" 

That should be posible for other objects as well.. As a solution I thought about iterating over the object and somehow take care of the namespace. But I wonder if there is an easy solution for that problem by using a library like jQuery or prototype.

like image 928
Luca Hofmann Avatar asked Sep 21 '12 16:09

Luca Hofmann


People also ask

How do you copy properties from one object to another in JavaScript?

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

How can we copy object without mutating?

Similar to adding elements to arrays without mutating them, You can use the spread operator to copy the existing object into a new one, with an additional value. If a value already exists at the specified key, it will be overwritten.

How do you duplicate something in JavaScript?

var clone = Object. assign({}, obj); The Object. assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object.

How do you update the properties of a JavaScript Object?

Updating Object Properties After you've created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update. For example, let's look at ourDog:

How to update JavaScript object with another object but only existing keys?

Update JavaScript object with another object, but only existing keys? For this, use hasOwnProperty (). Following is the code − To run the above program, you need to use the following command −

How to update an array of objects in JavaScript?

JavaScript offers two ways to update the object, using map () and findIndex (). This built-in array method, provided by JavaScript, iterates over the original array and creates the new array, completing the elements based on the specified conditions. Instead of an arrow function, you can also pass the callback function.

What is an updated value in JavaScript?

Think about what it does. It takes one of the references to an object and then it uses that reference to get access to the actual value stored inside of that object. And then it updates the existing value with a new value - "Updated value". In other words - it mutates the original object that we've declared at the very beginning of our code.


1 Answers

I suggest using underscore.js (or better, lo-dash) extend:

_.extend(destination, *sources)

Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.

_.extend({name: 'moe'}, {age: 50}); => {name: 'moe', age: 50} 
like image 150
bluehallu Avatar answered Sep 21 '22 16:09

bluehallu