Open In App

Lodash _.cloneDeepWith() Method

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:

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.




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




<!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:

Output

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


Article Tags :