JS: Object.assign
(new in JS: ECMAScript 2015)
Object.assign(target_obj, source_obj_1, source_obj_2, etc)
-
- Merge all source object keys into target object. (by Shallow copy)
- Only Enumerable own properties are considered.
- If property keys clash, later sources overwrites earlier ones.
- Return the modified target_obj.
Shallow copy means, if a source object contains a key whose value is object objX, the result will have objX as a reference, not creating new object, meaning, if objX is modified, it'll show up in both source and result object.
const jj = { pa: 99 }; // modify a existing object by merging new ones const aa = { pa: 0 }; const bb = { pb: 0 }; const cc = { pc: 0 }; const yy = Object.assign(jj, aa, bb, cc); console.log(JSON.stringify(yy) === `{"pa":0,"pb":0,"pc":0}`); // key a1 is overwritten