Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const a = {
  2.   name: 'Manuel',
  3.   age: 24,
  4.   address: {
  5.     street: 'Postplatz',
  6.     zip: 88250,
  7.     test: {
  8.       first: 'first',
  9.       second: 'second'
  10.     }
  11.   }
  12. }
  13.  
  14. function nestedCopy (obj) {
  15.   const clone = {}
  16.  
  17.   Object.keys(obj).map(key => {
  18.     if (typeof obj[key] === 'object') {
  19.       clone[key] = nestedCopy(obj[key])
  20.     } else {
  21.       clone[key] = obj[key]
  22.     }
  23.   })  
  24.  
  25.   return clone
  26. }
  27.  
  28. const b = nestedCopy(a)
  29.  
  30. setTimeout(() => {
  31.   b.address.zip = 25
  32.   b.address.test.first = 'last'
  33. }, 500)
  34.  
  35. setTimeout(() => {
  36.   console.log('A', a)
  37.   console.log('B', b)
  38. }, 1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement