Lodash _.cloneWith() 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 _.cloneWith() method of Lang in lodash is similar to _.clone() method and the only difference is that it accepts customizer which is called in order to generate cloned value. Moreover, if the customizer used here returns undefined then the cloning is dealt 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: This method accepts two parameters as mentioned above and described below:



  • value: It is the value to be compared.
  • customizer: It is the other value to be compared.

Return Value: This method returns cloned value.

Example 1:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring lodash library
const _ = require('lodash');
  
// Creating a function customizer
function customizer(val) {
  if (_.isElement(val)) {
    return val.cloneNode(false);
  }
}
  
// Defining value parameter
var value = [1, 2, 3];
  
// Calling cloneWith() method
var cloned_value = _.cloneWith(value, customizer);
  
// Displays output
console.log(cloned_value);

chevron_right


Output:

[ 1, 2, 3 ]

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring lodash library
const _ = require('lodash');
  
// Creating a function customizer
function customizer(val) {
  if (_.isElement(val)) {
    return val.cloneNode(false);
  }
}
  
// Defining value parameter
var value = ['Geeks', 'for', 'Geeks'];
  
// Calling cloneWith() method
var cloned_value = _.cloneWith(value, customizer);
  
// Displays output
console.log(cloned_value);

chevron_right


Output:

[ 'Geeks', 'for', 'Geeks' ]

Example 3: Using isInteger() method as customizer.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring lodash library
const _ = require('lodash');
  
// Creating a function int which acts as
// customizer
function int(x) {
 if (_.isInteger(x)) {
   return true;
   }
}
  
// Calling cloneWith() method along with
// its parameter
var cloned_value = _.cloneWith({ gfg: 5, 
        geeksforgeeks: 6}, int);
  
// Displays output
console.log(cloned_value);

chevron_right


Output:

{ gfg: 5, geeksforgeeks: 6 }

Reference: https://lodash.com/docs/4.17.15#cloneWith

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.