Skip to content

Instantly share code, notes, and snippets.

@zahmadsaleem
Created May 23, 2021 06:25
Show Gist options
  • Save zahmadsaleem/6e2c175d6e557fb2601d491baa0b72f5 to your computer and use it in GitHub Desktop.
Save zahmadsaleem/6e2c175d6e557fb2601d491baa0b72f5 to your computer and use it in GitHub Desktop.
Simple recursive deep clone function in JavaScript.
function deepClone(obj) {
let objectType = typeof obj;
if (objectType === "object" && obj !== null) {
return Object.entries(obj).reduce((acc, [key, val]) => {
acc[key] = deepClone(val);
return acc;
}, new obj.constructor());
} else {
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment