const deepClone = (obj: any) => {
if (obj === null) return null;
let clone = { ...obj };
Object.keys(clone).forEach(
(key) =>
(clone[key] = typeof obj[key] === "object" ? deepClone(obj[key]) : obj[key])
);
return Array.isArray(obj) && obj.length
? (clone.length = obj.length) && Array.from(clone)
: Array.isArray(obj)
? Array.from(obj)
: clone;
};
Created
March 18, 2024 21:11
-
-
Save dmonder/bf946a70825252c428de1c082fc36372 to your computer and use it in GitHub Desktop.
A TypeScript function for deep cloning objects and arrays recursively.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment