Created
November 20, 2018 20:08
-
-
Save tuvo1106/6defec0aa1181eec0ee51760c6595337 to your computer and use it in GitHub Desktop.
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
// deep clone | |
const deepClone = obj => { | |
// check to see if argument is an array/object and assign it to new variable | |
const newObj = (Array.isArray(obj) | |
? [] | |
: {}) | |
// loop through keys of object OR indexes of array | |
// Object.entries() work for arrays! | |
for (const [k, v] of Object.entries(obj)) { | |
// start deep clone | |
newObj[k] = (typeof v === 'object') | |
// if value is another array or object, recursively restart the process | |
? deepClone(v) | |
// if not, return value | |
: v | |
} | |
return newObj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment