Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DoctorDerek/a1f5aeaa7f2d5665ecf6523d232e7f67 to your computer and use it in GitHub Desktop.
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
// 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