Open In App

Lodash _.cloneDeepWith() Method

Last Updated : 03 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
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 src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js">
    </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=
    "https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js">
    </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

Next Article

Similar Reads