This post will discuss how to get a deep copy of an array in JavaScript.

Getting a deep copy of an array in JavaScript means creating a new array that contains the same values as the original array, but does not share any references with it. This way, modifying the new array will not affect the original array, and vice versa. There are several ways to get a deep copy of an array in JavaScript, depending on the type and complexity of the array elements. Here are some of the common functions:

1. Using JSON object functions

If the array elements are complex objects, such as nested arrays or objects with properties and functions, we can the JSON object functions to create a deep copy of the array. We can use JSON.stringify() converts the original array into a JSON string, and JSON.parse() converts the JSON string back into an array. Here’s an example:

Download  Run Code

 
Above example use the JSON object functions to create a deep clone of an array. This works well for simple arrays that only contain primitive values (numbers, strings, booleans) or nested arrays. However, this function only works if the array elements are compatible with the JSON format, which means they cannot contain functions, symbols, dates, or other non-serializable values. These values will be either lost or converted to strings. Here’s an example:

Download  Run Code

2. Using structuredClone() function

This is a modern way to deep copy an array in JavaScript that supports more data types than the JSON object functions. The structuredClone() function creates a structured clone of the array, which means it preserves the internal structure and references of the original array. This works well for arrays that contain complex objects, such as dates, maps, sets, including functions and symbols. Here’s an example:

Download Code

 
However, this function is not widely supported by all browsers yet. We can use the structuredClone() function to deep clone any type of array in modern browsers.

Download Code

3. Using Lodash library

If the array elements are not compatible with the JSON format, or if we want a more robust and efficient solution, we can use a third-party library like Lodash to create a deep copy of the array. Lodash provides the _.cloneDeep() function that recursively clones the array and its nested values. It also handles circular references and custom cloning logic. This requires importing the Lodash library into our project. Here’s an example:

Download Code

 
This function will recursively clone everything in the original array to the new array, including functions and symbols that are copied by reference. Here’s an example:

Download Code

4. Using slice() function

If the array elements are simple values, such as numbers, strings, or booleans, we can use the slice() function or the spread operator (…) to create a shallow copy of the array, which will also work as a deep copy in this case. In the below example, arr.slice() returns a new array with the same elements as the original array. This creates a shallow copy of the original array, but since the elements are primitive values, it effectively creates a deep copy.

Download  Run Code

That’s all about getting a deep copy of an array in JavaScript.