tl;dr: use speard operator for both arrays and objects for consistent syntax. Alternatives are Obect.assign()
and Array.concat()
Copying objects
1const original = { a: 1, b: 2 }
2
3// Object.assign()
4const copy1 = Object.assign({}, original)
5
6// Spread operator
7const copy2 = { ...original }
extending objects
1const original = { a: 1, b: 2 }
2const extension = { c: 3 }
3
4// Combine multiple objects with Object.assign()
5const extendedCopy1 = Object.assign({}, original, extension)
6
7// Combine multiple objects with Spread operator
8const extendedCopy2 = { ...original, ...extension }
Copying arrays
1// Copy array
2const original = [1, 2, 3]
3
4// .slice() with no args
5const copy = [1, 2, 3].slice()
extending arrays
1const original = [1, 2, 3]
2
3// Array.concat() returns a new array
4const extended = original.concat(4)
5const moreExtended = original.concat([4, 5])
6
7// Spread operator
8const extended2 = [...original, 3, 4]
9const moreExtended2 = [...original, ...extended2]
fyi: according to an article, Array.push()
is 945x faster than Array.concat()