Created
May 23, 2021 06:25
-
-
Save zahmadsaleem/6e2c175d6e557fb2601d491baa0b72f5 to your computer and use it in GitHub Desktop.
Simple recursive deep clone function in JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepClone(obj) { | |
let objectType = typeof obj; | |
if (objectType === "object" && obj !== null) { | |
return Object.entries(obj).reduce((acc, [key, val]) => { | |
acc[key] = deepClone(val); | |
return acc; | |
}, new obj.constructor()); | |
} else { | |
return obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment