Guest User

Untitled

a guest
Nov 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. // deep clone
  2. const deepClone = obj => {
  3. // check to see if argument is an array/object and assign it to new variable
  4. const newObj = (Array.isArray(obj)
  5. ? []
  6. : {})
  7. // loop through keys of object OR indexes of array
  8. // Object.entries() work for arrays!
  9. for (const [k, v] of Object.entries(obj)) {
  10. // start deep clone
  11. newObj[k] = (typeof v === 'object')
  12. // if value is another array or object, recursively restart the process
  13. ? deepClone(v)
  14. // if not, return value
  15. : v
  16. }
  17. return newObj
  18. }
Add Comment
Please, Sign In to add comment