Skip to content

Instantly share code, notes, and snippets.

@joelbarbosa
Last active March 11, 2020 10:05
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 joelbarbosa/8abec0d08469bc6377ae86bbd853251e to your computer and use it in GitHub Desktop.
Save joelbarbosa/8abec0d08469bc6377ae86bbd853251e to your computer and use it in GitHub Desktop.
const deepClone = obj => {
const clone = {}
for (let i in obj) {
if (obj[i] != null && typeof obj[i] === 'object') {
clone[i] = deepClone(obj[i])
} else {
clone[i] = obj[i]
}
}
return clone
}
const a = {
a: 'a',
b: {
b: 'b'
}
}
const c1 = deepClone(a)
c1.a = '1'
c1.b.b = '2'
console.log(c1, a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment