Sunday, November 17, 2024
Google search engine
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

Recent Comments