Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmonder/bf946a70825252c428de1c082fc36372 to your computer and use it in GitHub Desktop.
Save dmonder/bf946a70825252c428de1c082fc36372 to your computer and use it in GitHub Desktop.
A TypeScript function for deep cloning objects and arrays recursively.

Chrome Onboarding - Pieces for Developers Snippet

Preview:
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;
};
Associated Context
Type Code Snippet ( .ts )
Associated Tags deep clone object array recursive deepClone function object cloning recursive cloning null check spread operator Object keys
📝 Custom Description This is a snippet saved in the onboarding section of Pieces for Chrome. You can view relevant information about this snippet in the surrounding info boxes!
💡 Smart Description The code snippet defines a function called deepClone that creates a deep copy of an object. It recursively clones nested objects and arrays, ensuring that the cloned object is independent of the original object.
A TypeScript function for deep cloning objects and arrays recursively.
🔎 Suggested Searches JavaScript deep clone object function
Related Links https://code.visualstudio.com/docs/typescript/typescript-compiling
https://ultimatecourses.com/blog/all-about-immutable-arrays-and-objects-in-javascript
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html
https://code.pieces.app/onboarding/chrome/welcome
https://code.pieces.app/authenticated?os=a9f73b3e-adb4-4f85-9a79-626dfa32c804&user=b306468d-c2fd-45dc-a558-c99f2dc7f0fb
https://auth.pieces.services/u/reset-password/request/Username-Password-Authentication?state=hKFo2SAyelhaOFFxNncxczN0WGFxenNUQWFxUVFkc2dndFZGMaFurnJlc2V0LXBhc3N3b3Jko3RpZNkgYVIyN1pISVpkUnY4d3hwOVJGaWxPOGlJSE9Ca1AtWEyjY2lk2SAzbmd0S0FBWk14ZFQxMGwxNWdGRklPZ2hDZzRxZEI2Rw
https://code.pieces.app/developers-installed?os=4e08d9ab-4641-4271-8635-a581e86dbcd6&appVersion=8.0.0
Related People David Onder
Sensitive Information No Sensitive Information Detected
Shareable Link https://user-b306468d-c2fd-45dc-a558-c99f2dc7f0fb-gwcg3z6vmq-uk.a.run.app/?p=7d1943a54b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment