I want to add multiple attributes to an existing object with existing attributes. Is there a more concise way than one line per new attribute?
myObject.name = 'don';
myObject.gender = 'male';
Everything on MDN shows how to do new objects with bracket notation, but not existing objects: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With