Created
February 14, 2020 10:46
-
-
Save trojanh/379c4ef4b83053b1b5fb94a32a3a37db to your computer and use it in GitHub Desktop.
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
// deepClone.js | |
x = { a: 1, b: 2, z: { c: 3, d: 4, y: { e: 5 } } } | |
const deepClone = (obj) => { | |
if (typeof obj === "object" && obj !== null) { | |
return Object | |
.keys(obj) | |
.map((key) => { | |
return { | |
[key]: deepClone(obj[key]) | |
}; | |
}) | |
} else { | |
return obj | |
} | |
} | |
console.log(JSON.stringify (deepClone(x))); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment