how to update multiple object key value in javascript code example
Example 1: javascript update multiple values of an object
var myObject = {
label: 'foo',
name: 'bar',
id: 12
};
myObject = {...myObject, label: 'baz', name: 'qux'};
console.log(myObject);
var myUpdate = {
label: 'something',
name: 'else'
}
myObject = {...myObject, ...myUpdate}
console.log(myObject)
Example 2: Update matched key values in two JavaScript objects
var obj1={"id" : 1, "name" : "java"},
obj2={"id" : 1, "name" : "java4you", "gender" : "male"};
function compare(obj1, obj2) {
let obj = {};
for(let k in obj1) {
if(obj1[k] !== obj2[k]) {
obj = Object.assign({}, obj1, obj1[k] = obj2[k]);
}
}
return obj;
}