Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
- }
Add Comment
Please, Sign In to add comment