Created
April 24, 2022 21:18
-
-
Save DoctorDerek/f71798fd5498345ba3d0f4ce3253d397 to your computer and use it in GitHub Desktop.
Safe to Copy JSON Data: Primitive Types https://medium.com/p/c3f2783661e9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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