Created
April 29, 2019 12:08
-
-
Save Comandeer/04c3a44aa29b2d2511d1bc590e403914 to your computer and use it in GitHub Desktop.
Deep clone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepClone( obj ) { | |
const clone = { ...obj }; | |
Object.entries( clone ).forEach( ( [ key, value ] ) => { | |
if ( value && typeof value === 'object' ) { | |
clone[ key ] = deepClone( value ); | |
} | |
} ); | |
return clone; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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