Last active
March 11, 2020 10:05
-
-
Save joelbarbosa/8abec0d08469bc6377ae86bbd853251e 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
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