Open In App

Lodash _.cloneDeepWith() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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

Parameters:

  • 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.

Example 1: In this example, we are cloning the Head element by the use of the _.cloneDeepWith() method.

Javascript




<!DOCTYPE html>
<html>
 
<head>
    </script>
</head>
 
<body>
    <script type="text/javascript">
 
        function customizer(value) {
            if (_.isElement(value)) {
                return value.cloneNode(true);
            }
        }
 
        let 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: In this example, we are cloning the body element by the use of the _.cloneDeepWith() method.

Javascript




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
 
        function customizer(value) {
            if (_.isElement(value)) {
                return value.cloneNode(true);
            }
        }
 
        let 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:

Screenshot-2023-10-25-162750

Output

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



Last Updated : 31 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads