Copy

15 Sep 2023 . javascript .

Modify array

  1. .push() and .splice() directly modify the array

Return new array

  1. .concat() doesn’t modify array but just returns a new array
  2. .slice() doesn’t modify array but just returns a new array
  3. spread operator […array] doesn’t modify array but just returns a new array

Remove item

[...array.slice(0, index), ...array.slice(index + 1, array.length)]

arrray.slice(0, index).concat(array.slice(index + 1, array.length));

array.filter((_, i) => i !== index);

Copy Object

Take a target object and source objects and map properties from the source objects to the target object. Any matching properties are overwritten by properties in the source objects.

const newObject = Object.assign({}, obj1, obj2);