Lodash _.cloneWith() method of Lang in lodash is similar to the _.clone() method and the only difference is that it accepts a customizer which is called in order to generate cloned value. Moreover, if the customizer used here returns undefined then the cloning is dealt with by the method instead.
Note:
The customizer used here can be called with up to four arguments namely value, index|key, object, and stack.
Syntax:
_.cloneWith(value, [customizer]);
Parameters:
- value: It is the value to be compared.
- customizer: It is the other value to be compared.
Return Value:
This method returns a cloned value.
Example 1: In this example, we are cloning the given array with the customizer function and printing the result in the console.
Javascript
const _ = require( 'lodash' );
function customizer(val) {
if (_.isElement(val)) {
return val.cloneNode( false );
}
}
let value = [1, 2, 3];
let cloned_value = _.cloneWith(value, customizer);
console.log(cloned_value);
|
Output:
[ 1, 2, 3 ]
Example 2: In this example, we are cloning the given array with the customizer function and printing the result in the console.
Javascript
const _ = require( 'lodash' );
function customizer(val) {
if (_.isElement(val)) {
return val.cloneNode( false );
}
}
let value = [ 'Geeks' , 'for' , 'Geeks' ];
let cloned_value = _.cloneWith(value, customizer);
console.log(cloned_value);
|
Output:
[ 'Geeks', 'for', 'Geeks' ]
Reference: https://lodash.com/docs/4.17.15#cloneWith
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Nov, 2023
Like Article
Save Article