Skip to content

Instantly share code, notes, and snippets.

@15Dkatz
Created May 1, 2019 18:04
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 15Dkatz/f818e95d6fbf9516a3bdd4daf8ee1773 to your computer and use it in GitHub Desktop.
Save 15Dkatz/f818e95d6fbf9516a3bdd4daf8ee1773 to your computer and use it in GitHub Desktop.
An approach to deep cloning objects in JavaScript
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