SnipBits

How to clone an array in Javascript

There is too many ways to clone an Array in Javascript

  1. you can use the slice() method where it returns a copy of a portion of an array into a new array selected from begin to end
const clone = (arr) => arr.slice(0);
// clone(['1', '1']) === ['1', '1']
  1. you can use the spread operator that allows an iterable such as an array expression or string to be expanded in places
const clone = (arr) => [...arr];
// clone(['1', '1']) === ['1', '1']
  1. The from() method creates a new Array instance from an array or iterable object.
const clone = (arr) => Array.from(arr);

// clone(['1', '1']) === ['1', '1']
  1. You can use The map() method that creates a new array with the results of calling a provided function on every element in the calling array.
const clone = (arr) => arr.map((x) => x);

// clone(['1', '1']) === ['1', '1']
  1. you can use an old method where we converts a JavaScript array to a JSON string and then you parse it again

you can read more about the JSON Methods here

const clone = JSON.parse(JSON.stringify(arr));

// clone(['1', '1']) === ['1', '1']

Maher Gallaoui

You like our Content? Follow us to stay up-to-date.