How to Deep Copy Array Using Lodash

In this article, you will learn how to deep copy an array using Lodash. Let’s say you have an array named ‘a’ with elements ‘1,…

The post How to Deep Copy Array Using Lodash appeared first on CodeSource.io.

In this article, you will learn how to deep copy an array using Lodash.

Let’s say you have an array named ‘a’ with elements ‘1, 2, 3, 4’.

// Import Lodash library
import _ from "lodash";

var a = [1, 2, 3, 4];

In order to deep copy an array using Lodash, you can use the _.cloneDeep() method.

// Import Lodash library
import _ from "lodash";

var a = [1, 2, 3, 4];

// Create a deep copy of the previous array
var b = _.cloneDeep(a);

// Change the first element of array 'b' to 9
b[0] = 9;

console.log(a);
// => [1, 2, 3, 4]

console.log(b);
// => [9, 2, 3, 4]

Note: The _.cloneDeep() method functions by creating a fully independent yet exact copy of supplied values.

The post How to Deep Copy Array Using Lodash appeared first on CodeSource.io.


Print Share Comment Cite Upload Translate
APA
Ariessa Norramli | Sciencx (2024-03-29T15:57:35+00:00) » How to Deep Copy Array Using Lodash. Retrieved from https://www.scien.cx/2021/02/19/how-to-deep-copy-array-using-lodash/.
MLA
" » How to Deep Copy Array Using Lodash." Ariessa Norramli | Sciencx - Friday February 19, 2021, https://www.scien.cx/2021/02/19/how-to-deep-copy-array-using-lodash/
HARVARD
Ariessa Norramli | Sciencx Friday February 19, 2021 » How to Deep Copy Array Using Lodash., viewed 2024-03-29T15:57:35+00:00,<https://www.scien.cx/2021/02/19/how-to-deep-copy-array-using-lodash/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Deep Copy Array Using Lodash. [Internet]. [Accessed 2024-03-29T15:57:35+00:00]. Available from: https://www.scien.cx/2021/02/19/how-to-deep-copy-array-using-lodash/
CHICAGO
" » How to Deep Copy Array Using Lodash." Ariessa Norramli | Sciencx - Accessed 2024-03-29T15:57:35+00:00. https://www.scien.cx/2021/02/19/how-to-deep-copy-array-using-lodash/
IEEE
" » How to Deep Copy Array Using Lodash." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/19/how-to-deep-copy-array-using-lodash/. [Accessed: 2024-03-29T15:57:35+00:00]
rf:citation
» How to Deep Copy Array Using Lodash | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/02/19/how-to-deep-copy-array-using-lodash/ | 2024-03-29T15:57:35+00:00
https://github.com/addpipe/simple-recorderjs-demo