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.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/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.
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdn.jsdelivr.net/npm/lodash@4.17.11/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:
