Thursday, September 4, 2025
HomeLanguagesJavascriptLodash _.cloneDeepWith() Method

Lodash _.cloneDeepWith() Method

The Lodash _.cloneDeepWith() method is used to clone value in a recursive way, just the same as the  _.cloneWith() method but performs in a recursive manner.

Syntax:

_.cloneDeepWith( value, customizer )

Parameter: This method accepts two parameters as mentioned above and described below:

  • value: This parameter holds the value which will be cloned in a recursive way.
  • customizer: This parameter holds the function to customize the clone.

Return value: This method returns the cloned value.

Below example illustrate the Lodash _.cloneDeepWith() Method:

Example 1: Cloning Head element.

Javascript




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
 
        function customizer(value) {
            if (_.isElement(value)) {
                return value.cloneNode(true);
            }
        }
  
        var gfg = _.cloneDeepWith(document.head, customizer);
          
        console.log(gfg === document.head);
        console.log(gfg.nodeName);
        console.log(gfg.childNodes.length);
        console.log(gfg);
    </script>
</body>
 
</html>


Output:

Example 2: Cloning body element.

Javascript




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
 
        function customizer(value) {
            if (_.isElement(value)) {
                return value.cloneNode(true);
            }
        }
          
        var gfg = _.cloneDeepWith(document.body, customizer);
          
        console.log(gfg === document.body);
        console.log(gfg.nodeName);
        console.log(gfg.childNodes.length);
        console.log(gfg);
    </script>
</body>
 
</html>


Output:

Reference: https://docs-lodash.com/v4/clone-deep-with/

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11860 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS