ref –
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
Literal Objects
1 |
let objClone = { ...obj }; // pass all key:value pairs from an object |
Arrays
1 |
[...iterableObj, '4', 'five', 6]; // combine two arrays by inserting all elements from iterableOb |
Spread in Array Literals
1 2 |
let parts = ['shoulders', 'knees']; let lyrics = ['head', ...parts, 'and', 'toes']; |
[“head”, “shoulders”, “knees”, “and”, “toes”]
Spread in Object Literals
1 2 3 4 5 6 7 8 |
let obj1 = { foo: 'bar', x: 42 }; let obj2 = { foo: 'baz', y: 13 }; let clonedObj = { ...obj1 }; // Object { foo: "bar", x: 42 } let mergedObj = { ...obj1, ...obj2 }; // Object { foo: "baz", x: 42, y: 13 } |