A library is just a collection of solutions written in a way that they can be reusable. Here is the module in lodash that contains the logic for the deep clone github.com/lodash/lodash/blob/mast..., it's imported from this module github.com/lodash/lodash/blob/mast..., which is the module you would use in your code.
Passionate about building great technology and connecting with people to create positive change. Happy to answer questions about transitioning to tech. Find me on Twitter @lounecl
There seems to be no native way, but it is as simple as,
functiondeepClone(o){/**
* This excludes null
*/if(o&&typeofo==='object'){if(Array.isArray(o)){returno.map((a)=>deepClone(a))}elseif(o.constructor===Object){returnObject.entries(o).reduce((prev,[k,v])=>({...prev,[k]:deepClone(v)}),{})}/**
* Now it depends on how you would recreate the Object, or not...
*/returno}/**
* BTW, function is another important case you might consider...
*/returno}
A perfect way is simply,
/**
* Yes, that dangerousLoad
* js-yaml's dump also supports several options
*/yaml.load(yaml.dump(o))
Passionate about building great technology and connecting with people to create positive change. Happy to answer questions about transitioning to tech. Find me on Twitter @lounecl
Passionate about building great technology and connecting with people to create positive change. Happy to answer questions about transitioning to tech. Find me on Twitter @lounecl
A library is just a collection of solutions written in a way that they can be reusable. Here is the module in lodash that contains the logic for the deep clone github.com/lodash/lodash/blob/mast..., it's imported from this module github.com/lodash/lodash/blob/mast..., which is the module you would use in your code.
Are you means?
test cases (mandatory true)
This is awesome! Thanks for also adding the test cases
There seems to be no native way, but it is as simple as,
A perfect way is simply,
This is helpful. Thanks!!
If it's not a nested object, it's really nice to use the spread operator
const newObject = { ..currentObject };
If it's a nested or deeply nested object, it's much safer to use
const newObject = JSON.parse(JSON.stringify(currentObject))
in a nutshell:
const cloneDeep = (obj) => {
return JSON.parse(JSON.stringify(obj))
}
I should say i haven't taken the time to see any performance detriments, if any, appear.
cloning nested object is like diving into a black hole
developer.mozilla.org/en-US/docs/W...
let obj = {stuff: {
more_stuff: "this",
list: [1,2,3]},
}
JSON.parse(JSON.stringify(obj))
This is only a shallow clone so nested reference types would not be cloned