Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DoctorDerek/f71798fd5498345ba3d0f4ce3253d397 to your computer and use it in GitHub Desktop.
Save DoctorDerek/f71798fd5498345ba3d0f4ce3253d397 to your computer and use it in GitHub Desktop.
Safe to Copy JSON Data: Primitive Types https://medium.com/p/c3f2783661e9
const safeToCopy = ["1", 2, [true]]
const goodCopy = JSON.parse(JSON.stringify(safeToCopy))
console.info(safeToCopy) // ["1", 2, [true]]
console.info(goodCopy) // ["1", 2, [true]]
// Since we're working with JSON-safe data, the deep copy is valid:
const copyDeux = JSON.parse(JSON.stringify(safeToCopy))
copyDeux[2][0] = false // Change deeply-nested [true] to [false]
console.info(safeToCopy) // ["1", 2, [true]]
console.info(copyDeux) // ["1", 2, [false]]
// The change to the "deep copy" didn't affect the original array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment