Skip to content

Instantly share code, notes, and snippets.

@trojanh
Created February 14, 2020 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trojanh/379c4ef4b83053b1b5fb94a32a3a37db to your computer and use it in GitHub Desktop.
Save trojanh/379c4ef4b83053b1b5fb94a32a3a37db to your computer and use it in GitHub Desktop.
// 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