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

You can also try this code with Online Javascript Compiler
Run Code
Output
true
false
'normal value'
Demonstrates customizing the cloning of date properties in an object.
Handling Custom Objects During Cloning:
JavaScript
function CustomType(value) {
this.value = value;
}
var customObject = new CustomType('original');
var clonedCustom = _.cloneWith(customObject, (val) => {
if (val instanceof CustomType) return new CustomType('cloned');
});
console.log(clonedCustom.value);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'cloned'
Shows custom handling for a custom object type during cloning.
Transforming Cloned Values:
JavaScript
var data = { num: 1, str: 'hello' };
var transformClone = (val) => {
if (_.isString(val)) return val.toUpperCase();
};
var clonedData = _.cloneWith(data, transformClone);
console.log(clonedData.num);
console.log(clonedData.str);

You can also try this code with Online Javascript Compiler
Run Code
Output
1
'HELLO'
An example of transforming values during the cloning process.
Excluding Certain Properties from Cloning:
JavaScript
var complexObject = { a: 1, b: { c: 2 }, d: [3, 4] };
var excludeB = (val, key) => {
if (key === 'b') return {};
};
var clonedObject = _.cloneWith(complexObject, excludeB);
console.log(clonedObject.b);
console.log(clonedObject.d);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{}
[3, 4] (unchanged)
Demonstrates excluding a specific property during cloning while keeping others intact.
Frequently Asked Questions
How does _.cloneWith() differ from _.clone()?
_.cloneWith() allows for a customizer function to define specific cloning behavior for elements, whereas _.clone() performs a standard shallow clone without customization.
What types of values can be customized with _.cloneWith()?
The customizer function in _.cloneWith() can target any type of value within the object or array, including nested objects, arrays, and primitive values.
Does _.cloneWith() handle deep cloning?
No, _.cloneWith() performs a shallow clone. For deep cloning with customization, use _.cloneDeepWith().
Conclusion
Lodash's _.cloneWith() method offers an effective way to create shallow copies of objects or arrays while providing the flexibility to customize the cloning process. It's particularly useful for handling special data types or applying specific transformations during cloning.
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.