How to Deep Clone an Array in JavaScript

  • javascript
  • 73 Views
  • 4 Min Read
  • 9 Jul 2024

It is important to understand how to clone arrays in JavaScript to manage data more effectively. In this article, we will learn how to do deep cloning in JavaScript. There are many types of deep cloning, and we have covered them all. So let's enhance your skills in JavaScript.

 

Let's begin!

 

Table of contents

 

  1. JavaScript Array Cloning Techniques
  2. Deep Clone Array JavaScript
  3. JSON vs Lodash Array Cloning
  4. Best Practices for Deep Cloning Arrays

 

 

1. JavaScript Array Cloning Techniques

 

Arrays in JavaScript are fundamental data structures used to store collections of elements. When you need to duplicate an array, ensure that modifications made to one array don't affect the original. At that time, array cloning techniques come into play.

 

There are primarily two types of copying arrays or objects: shallow copy and deep copy. A shallow copy of an array only makes a copy of the outermost layer, leaving the inner arrays linked to the original. Not understood? Don't worry, see the example for easy understanding.

 
let originalArray = [1, 2, [3], 4];

let copiedArray = [...originalArray]; // create copy with shallow copy

// Change the element of the array(outermost layer) and in the nested array
copiedArray[0] = 10;
copiedArray[2][0] = 20;

// Original array affected
console.log(originalArray); // [ 1, 2, [ 20 ], 4 ]
console.log(copiedArray); // [ 10, 2, [ 20 ], 4 ]
 

In this example, we have an array originalArray consisting of elements [1, 2, [3], 4]. Using the spread operator, we create a shallow copy named copiedArray. When we modify copiedArray[0] to 10, it doesn't affect originalArray as it only changes the top-level element.

 

But, when modifying the nested array, like copiedArray[2][0] to 20, it will impact both the originalArray and the copiedArray. This is because copiedArray is a shallow copy, which duplicates only the top level of the array without altering the original. Consequently, changes made to nested elements are reflected in both arrays. As a result, console.log(originalArray) outputs [1, 2, [20], 4], while console.log(copiedArray) outputs [10, 2, [20], 4].

 

If we use Object.assign() or the spread operator (...) to copy an array, it means we are using shallow copy. So, if the array contains nested or multi-dimensional elements, modifying the copied array will also affect the original array.

 

If you want a completely independent copy of all nested arrays, you need to use a deep clone. Let's see how we can use deep copy for copying arrays.

 

 

2. Deep Clone Array JavaScript

 

There are many ways to perform deep cloning in JavaScript, but I will teach you 3 ways with the code example that are most used and also easy.

 

  • Using JSON methods
  • Using a library like Lodash:
  • Using custom recursive function:
 
Example: (using the JSON method)
 
let originalArray = [1, 2, [3], 4];

// Create a copy with a deep copy
let arrayCopy = JSON.parse(JSON.stringify(originalArray));

// Change element of the array and in the nested array
copiedArray[0] = 10;
copiedArray[2][0] = 20;

// Original not affected
console.log(originalArray); // [ 1, 2, [ 3 ], 4 ]
console.log(copiedArray);   // [ 10, 2, [ 20 ], 4 ]

 

In this code snippet, we have an array originalArray containing elements [1, 2, [3], 4]. To create a deep copy of this array, we use the JSON.parse(JSON.stringify(originalArray)) method, resulting in copiedArray.

 

When we modify copiedArray[0] to 10 and copiedArray[2][0] to 20, it does not affect the original originalArray. This is because copiedArray is a deep copy, meaning it replicates the entire structure of the array, including nested arrays, without referencing the original. As a result, console.log(originalArray) outputs [1, 2, [3], 4], while console.log(copiedArray) outputs [10, 2, [20], 4].

 

Now you should understand how deep copy works and what the difference is between shallow and deep copy. Now, we can take a different approach to get the same result. The output is the same, but we have an additional choice.

 

Example: (using a library like Lodash)

 
const _ = require('lodash');

const originalArray = [1, [2], [3, [4]], 5];

const copiedArrayWithLodas = _.cloneDeep(originalArray);

copiedArrayWithLodas[1][0] = 10;
copiedArrayWithLodas[2][1][0] = 8;

console.log(originalArray);        // [ 1, [ 2 ], [3, [4] ], 5 ]
console.log(copiedArrayWithLodas); // [ 1, [ 10 ], [3, [8] ], 5 ]
 
In this code, the Lodash library makes an independent copy of an array called originalArray. After making a copy named copiedArrayWithLodash, we change some values inside it: we set copiedArrayWithLodash[1][0] to 10, and copiedArrayWithLodash[2][1][0] to 8.
 
When we print both originalArray and copiedArrayWithLodash, we can see that the original array is still the same; only the copied array value has been changing since we made it.
 
Example: (using a custom Recursive Function)
 
function deepCloneArray(arr) {
    return arr.map(item => Array.isArray(item) ? deepCloneArray(item) : item);
}

const originalArray = [1, [2], [3, [4]], 5];

const copiedArrayRecursiveFunc = deepCloneArray(originalArray);

copiedArrayRecursiveFunc[0] = 10
copiedArrayRecursiveFunc[1][0] = 20

console.log(originalArray);            // [ 1, [ 2 ], [ 3, [ 4 ] ], 5 ]
console.log(copiedArrayRecursiveFunc); // [ 10, [ 20 ], [ 3, [ 4 ] ], 5 ]
 

This code copies arrays using a function called deepCloneArray. First, it makes a copy of an array called originalArray and stores it in copiedArrayRecursiveFunc using deepCloneArray. Then, it changes the first item in copiedArrayRecursiveFunc to 10 and the first item in its nested array to 20. After that, it shows both arrays. The originalArray stays the same, but copiedArrayRecursiveFunc shows the changes made.

 

 

4. JSON vs Lodash Array Cloning

 

When you're deciding whether to use JSON or Lodash to copy arrays, think about what your project needs. Here are some helpful suggestions to guide you in making the right choice.

 

JSON Array Cloning:

 

  • Optimal for simplicity if you require basic, shallow copying.
  • Best for scenarios where you want to avoid external dependencies.
  • However, it's limited to shallow copying and may not handle complex arrays well.

 

Lodash Array Cloning:

 

  • Choose Lodash for more advanced array manipulation needs.
  • Ideal for deep cloning or managing non-serializable values.
  • Offers a comprehensive set of tools for array operations.
  • Keep in mind that it adds a dependency to your project and may increase bundle size.
 

 

5. Best Practices for Deep Cloning Arrays

 

Using Lodash's cloneDeep() function: Utilize dedicated tools like Lodash's cloneDeep() function, designed specifically for deep cloning arrays. These libraries offer robust solutions with well-tested implementations.

 

Implementing custom recursive functions: If you prefer not to use external libraries, you can create your own recursive function to deep-clone arrays. Ensure the function properly handles nested arrays and objects.

 

Avoiding JSON methods for deep cloning: While JSON.parse(JSON.stringify(array)) can be used for shallow cloning, it is not suitable for deep cloning as it does not preserve non-serializable values like functions or symbols.

 

Thorough testing: Extensive testing across various scenarios helps verify the correctness and robustness of your deep cloning implementation, ensuring it handles edge cases effectively.

 

Document comprehensively: Documenting your deep cloning process within your codebase enhances readability and facilitates maintenance, ensuring other developers can understand and modify the code as needed.

 

 

Conclusion

 

It is important to understand deep cloning for data manipulation. We have told you three ways to use deep cloning, which you can use according to your project. Make sure to practice what we've covered; it'll help you get a better grasp of the idea.

 

If you have any questions regarding this article or any other web development, then you can ask them using the question box given below, and you will get the answer soon.

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!

About Author

Username

Diya Jain ( diya )

The Dev without a Degree

Joined On 10 Apr 2024

More from Diya Jain

10 Fun Websites for Stress Relief and Relaxation During Coding Breaks

programming

11 Nov 2024

Top 10 fun websites for coders to relax during breaks. Recharge with interactive games, ar....

How to Implement Undo Functionality for Deleting Items in Your React App

react-js

28 Sep 2024

Learn how to implement undo functionality for deleting items in React. Follow a step-by-st....

Top Strategies for Search Bar Optimization on Major Platforms

web-development

6 Sep 2024

Discover top strategies for search bar optimization on major platforms. Enhance real-time ....

Top Strategies to Optimize ReactJS Application Performance for Faster Load Times

react-js

23 Aug 2024

From this article, you will discover the best practices to enhance the speed of loading yo....

Comprehensive Guide to Tooltips in ReactJS

react-js

5 Jun 2024

Explore ReactJS tooltips from start to finish! Learn how to add, customize, troubleshoot, ....

Comprehensive Guide to React Hooks: When and How to Use Each Hook Effectively

react-js

9 Jul 2024

In React, we use a component-based structure. A component is like a building block of code....

Popular Posts from Code Mafias

10 Fun Websites for Stress Relief and Relaxation During Coding Breaks

programming

11 Nov 2024

Top 10 fun websites for coders to relax during breaks. Recharge with interactive games, ar....

How to Create Advanced Search Bar with Suggestions in React: A Step-by-Step Guide

react-js

2 Aug 2024

Learn to build an advanced React search bar with suggestions, highlighted text, efficient ....

Mastering HTML: Top 12 Unique HTML Tags with Examples

html

4 May 2024

Through this article, learn those essential HTML tags that many developers do not know abo....

Top 60 Eye-Catching Button Designs Users Can’t Wait to Click - With Source Code

ui-ux

11 Oct 2024

Discover 60 eye-catching button designs with source code, crafted with HTML and CSS to enh....

Top Strategies for Search Bar Optimization on Major Platforms

web-development

6 Sep 2024

Discover top strategies for search bar optimization on major platforms. Enhance real-time ....

How to Upload Code to GitHub: Quick Steps and Detailed Instructions for Beginners

github

16 Sep 2024

In order to upload (push) your project to GitHub, it involves multiple steps that need to ....