Created
May 1, 2019 18:04
-
-
Save 15Dkatz/f818e95d6fbf9516a3bdd4daf8ee1773 to your computer and use it in GitHub Desktop.
An approach to deep cloning objects in JavaScript
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
const cloneDeep = source => { | |
const result = {}; | |
Object.entries(source).forEach(entry => { | |
const [key, value] = entry; | |
if (typeof value === 'object') { | |
result[key] = cloneDeep(value); | |
} else { | |
result[key] = value; | |
} | |
}); | |
} | |
// Note that this doesn't support arrays. It turns arrays into their object form. | |
// ['foo', 'bar'] becomes {0: 'foo', 1: 'bar'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment