Created
April 24, 2022 01:24
-
-
Save DoctorDerek/a1f5aeaa7f2d5665ecf6523d232e7f67 to your computer and use it in GitHub Desktop.
Example of lodash.clonedeep using require instead of import to deep copy https://medium.com/p/3242c9eae48
This file contains hidden or 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
// eslint-disable-next-line @typescript-eslint/no-var-requires | |
const lodashClonedeep = require("lodash.clonedeep") | |
const array = [1337, { x: "✅" }, { y: { z: "⚡" } }] // deeply nested array | |
const copy = lodashClonedeep(array) // shallow copy with lodashClonedeep | |
console.info(array) // [1337, { x: "✅" }, { y: { z: "⚡" } }] | |
copy[0] = "leet" // change a primitive value (not nested) | |
copy[1].x = "❎" // change a deeply nested value | |
copy[2].y.z = "❌" // change another deeply nested value | |
console.info(array) // [1337, { x: "✅" }, { y: { z: "⚡" } }] | |
console.info(copy) // ["leet", { x: "❎" }, { y: { z: "❌" } }] | |
// Try this code sample at https://npm.runkit.com/lodash.clonedeep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment