Skip to content

Instantly share code, notes, and snippets.

@ALEXOTANO
Created August 14, 2019 19:58
Show Gist options
  • Save ALEXOTANO/f31ab7342a064639c4cf6ea04836bbc3 to your computer and use it in GitHub Desktop.
Save ALEXOTANO/f31ab7342a064639c4cf6ea04836bbc3 to your computer and use it in GitHub Desktop.
The best way to deep clone an object / array of objects in javasciprt

When an object has more objects inside, Object.Assign doesn't do the trick.

The easist way to do it:

var originalObject = { 
  p: new Date(), 
  a: 123, 
  otherObj: { 
    m: 'more data', 
    objOtherOne: {  
      c: 'x' 
    } 
  } 
}
var copy2 = JSON.parse(JSON.stringify(originalObject));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment