Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Created November 14, 2020 14:56
Show Gist options
  • Save SimonHoiberg/b2a4593d97e60947569df6f78a00b45b to your computer and use it in GitHub Desktop.
Save SimonHoiberg/b2a4593d97e60947569df6f78a00b45b to your computer and use it in GitHub Desktop.
Create a deep clone of an object
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