Add multiple attributes to an existing js object

I'm not sure if that's helpful, but there is a trick using JS ES6 spread syntax:

let obj = {a: 1, b: 2};
obj = {...obj, b: 3, c: 4};
console.log(obj); // {a: 1, b: 3, c: 4}

You can try to use this somehow... In general, that's a single liner to add or override object properties using js destructuring method.

Edit: Notice that the placement of ...obj in the new object is cruicial, as placing it first would allow overriding it with the following params (b: 3, c: 4 in my example), and placing it last will give priority to the members that are already in the object.


In ES6/ ES2015 you can use the Object.assign method

let obj = {key1: true};
console.log('old obj: ', obj);
let newObj = {key2: false, key3: false};

Object.assign(obj, newObj);
console.log('modified obj: ', obj);