Saturday, February 7, 2026
HomeLanguagesJavascriptLodash _.cloneWith() Method

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




// 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);


Output:

[ 1, 2, 3 ]

Example 2:

Javascript




// 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);


Output:

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

Example 3: Using isInteger() method as customizer.

Javascript




// 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, 
        neveropen: 6}, int);
  
// Displays output
console.log(cloned_value);


Output:

{ gfg: 5, neveropen: 6 }

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, neveropen 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!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32492 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6863 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12079 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7239 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6934 POSTS0 COMMENTS