Skip to content

Instantly share code, notes, and snippets.

@Comandeer
Created April 29, 2019 12:08
Show Gist options
  • Save Comandeer/04c3a44aa29b2d2511d1bc590e403914 to your computer and use it in GitHub Desktop.
Save Comandeer/04c3a44aa29b2d2511d1bc590e403914 to your computer and use it in GitHub Desktop.
Deep clone
function deepClone( obj ) {
const clone = { ...obj };
Object.entries( clone ).forEach( ( [ key, value ] ) => {
if ( value && typeof value === 'object' ) {
clone[ key ] = deepClone( value );
}
} );
return clone;
}
function deepClone( obj ) {
const transformed = Object.entries( obj ).map( ( [ key, value ] ) => {
return [ key, value && typeof value === 'object' ? deepClone( value ) : value ];
} );
return Object.fromEntries( transformed );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment