The Wayback Machine - https://web.archive.org/web/20240613234830/https://www.geeksforgeeks.org/lodash-_-clonedeep-method/
Open In App

Lodash _.cloneDeep() Method

Last Updated : 10 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Lodash _.cloneDeep() method is handy for making a complete copy of a value. It goes deep into the value, copying everything inside it too. So, the new object you get has the exact same data as the original, but they’re not linked in memory.

This method is similar to the _.clone() method.

Syntax: 

_.cloneDeep(value);

Parameters:

  • value parameter holds the value that needs to be cloned recursively.

Return Value:

This method returns the deep-cloned value

Example 1: In this example, It is returning false because they both have the same value but different memory allocations, and the “===” operator checks for the same reference in objects

Javascript
const _ = require('lodash');

let obj = {
    x: 23
};
// Deep copy 
let deepCopy = _.cloneDeep(obj);

console.log('Comparing original with'
    + ' deep ', obj === deepCopy);

obj.x = 10; // Changing original value 

console.log('After changing original value');

console.log("Original value ", obj);

console.log("Deep Copy value ", deepCopy); 

Output: 

Comparing original with deep  false
After changing original value
Original value  { x: 10 }
Deep Copy value  { x: 23 }

Example 2: In this example, It is returning false because they both have the same value but different memory allocations, and “===” operator checks for the same reference in objects and we can change the original object it will not change the cloned object’s value as both of them have different memory allocation

Javascript
const _ = require('lodash');

let obj = [{ x: 1 }, { y: 2 }];

// Deep copy 
let deepCopy = _.cloneDeep(obj);

console.log('Comparing original with deep ',
    obj[0] === deepCopy[0]);

// Changing original value 
obj[0].x = 10;

// Values after changing original value 
console.log("After changing original value");

console.log("Original value ", obj);

console.log("Deep Copy value ", deepCopy); 

Output: 

Comparing original with deep  false
After changing original value
Original value  [ { x: 10 }, { y: 2 } ]
Deep Copy value  [ { x: 1 }, { y: 2 } ] 

Previous Article
Next Article

Similar Reads

Lodash _.method() Method
Lodash _.method() method creates a function that invokes the method at the path of a given object. Any additional arguments are provided to the invoked method. Syntax: _.method(path, args);Parameters: path: This is the path to invoke.args: These are the arguments to invoke the method with.Return Value: This method returns the new invoker function.
1 min read
Lodash _.flattenDeep() and _.flattenDepth() Method
Lodash _.flattenDeep() Method The _.flattenDeep() method is used to completely flatten nested arrays. It does this recursively. Syntax: _.flattenDeep( array ) Parameters: This method accepts single parameter as mentioned above and described below: array: This parameter holds the array that to be flatten. Return Value: This method returns the new fl
2 min read
Lodash _.dropWhile() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers etc. The Loadsh.dropWhile() method is used to return the slice of the given array. This takes a predicate function that iterate through each element of the array and if the function returns false, it returned the sli
2 min read
Lodash _.stubFalse() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.stubFalse() method is used to always return a false value. The basic difference between using a normal false value and using the _.stubFalse() method is that lambda functions create a new different functi
1 min read
Lodash _.pullAllBy() Method
The _.pullAllBy() method is used to remove the values from the original array by iterating over each element in the array by using the Iteratee function. It is almost the same as _.pullAll() function. Syntax: _.pullAllBy(array, values, [iteratee=_.identity]) Parameters: This method accepts two parameters as mentioned above and described below: arra
2 min read
Lodash _.sortedLastIndexOf() Method
The _.sortedLastIndexOf method is used to return the highest index of the array where an element can be inserted and maintain its sorted order. Also this method is like _.lastIndexOf except that it performs a binary search on a sorted array.Syntax: _.sortedLastIndexOf(array, value) Parameters: This method accepts two parameters as mentioned above a
1 min read
Lodash _.isSymbol() Method
The _.isSymbol() method is used to find whether the given value is a symbol object or not. It returns True if the given value is a symbol object. Otherwise, it returns false. Syntax: _.isSymbol(value) Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter holds the value to check. Return Val
1 min read
Lodash _.toArray() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.toArray() method is used to convert the given value to an array. Syntax: _.toArray( value ) Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter
1 min read
Lodash _.reduce() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc. The _.reduce() method reduces collection to value which is accumulated result of running each element in the collection through iteratee, where each successive invocation is supplied return value of
2 min read
Lodash _.splitAt() Method
The Lodash _.splitAt() method takes an array and a numeric index and returns a new array containing two embedded arrays made by splitting the original array at the provided numeric index. Syntax: _.splitAt(array, numeric_index) Parameters: This method takes two parameters as mentioned above and described below: array: The array to be split.numeric_
3 min read