-
-
Save SimonHoiberg/b2a4593d97e60947569df6f78a00b45b to your computer and use it in GitHub Desktop.
Create a deep clone of an object
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) => { | |
let clone = obj; | |
if (obj && typeof obj === "object") { | |
clone = new obj.constructor(); | |
Object.getOwnPropertyNames(obj).forEach((property) => { | |
clone[property] = deepClone(obj[property]); | |
}); | |
} | |
return clone; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment