Syntax, Parameter and Return Value
Syntax:
_.cloneDeepWith(value, [customizer])
Parameters:
-
value (Object|Array): The value to deeply clone.
- [customizer] (Function): The function to customize cloning each element.
Return Value:
Returns the deeply cloned value with custom behavior applied.
Examples
Custom Cloning of Special Objects:
JavaScript
var _ = require('lodash');
var customCloner = function(value) {
if (value instanceof Date) {
return new Date(value);
}
};
var object = { a: new Date(2020, 1, 1), b: 'normal value' };
var clonedObject = _.cloneDeepWith(object, customCloner);
console.log(clonedObject.a instanceof Date);
console.log(clonedObject.a === object.a);

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
false
Demonstrates custom cloning for Date objects within a complex object.
Handling Custom Class Instances:
JavaScript
function MyClass(value) {
this.value = value;
}
var myObject = new MyClass(10);
var clonedMyObject = _.cloneDeepWith(myObject, (val) => {
if (val instanceof MyClass) return new MyClass(val.value);
});
console.log(clonedMyObject instanceof MyClass); // Output: true
console.log(clonedMyObject.value);

You can also try this code with Online Javascript Compiler
Run Code
Output:
10
Shows custom handling for instances of a custom class during cloning.
Transforming Nested Objects:
JavaScript
var data = {
nested: { num: 1, str: 'hello' }
};
var transformNested = (val) => {
if (_.isObject(val)) return _.mapValues(val, v => v.toString());
};
var clonedData = _.cloneDeepWith(data, transformNested);
console.log(clonedData.nested.num);
console.log(clonedData.nested.str);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'1'
'hello'
An example of transforming nested objects' values to strings during cloning.
Excluding Specific Properties:
JavaScript
var complexObject = {
a: 1,
b: { c: 2, d: 3 },
e: [4, 5, 6]
};
var excludeB = (val) => {
if (_.isEqual(val, complexObject.b)) return {};
};
var clonedWithoutB = _.cloneDeepWith(complexObject, excludeB);
console.log(clonedWithoutB.b);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{}
Demonstrates excluding a specific property from being deeply cloned.
Frequently Asked Questions
How does _.cloneDeepWith() differ from _.cloneDeep()?
_.cloneDeepWith() allows for a customizer function that defines specific cloning behavior for each element, whereas _.cloneDeep() performs a standard deep clone without customization.
What types of values can be customized with _.cloneDeepWith()?
Any type of value within the object or array being cloned can be customized, including objects, arrays, primitives, and special objects like Date or custom class instances.
Can _.cloneDeepWith() handle circular references?
Handling circular references requires custom logic in the customizer function. Without it, _.cloneDeepWith() may result in a stack overflow error for objects with circular references.
Conclusion
Lodash's _.cloneDeepWith() method offers a powerful and flexible approach to deep cloning, providing the ability to customize the cloning process for each element. It is especially valuable when working with complex data structures or specific types that require tailored cloning logic.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.