This content originally appeared on DEV Community and was authored by James Thomson
If you've ever wondered "why doesn't Javascript have a method to deep clone?", you wouldn't be the only one.
For years we've been using various methods or libraries to clone objects and it's never felt quite right. Thankfully, structuredClone
is coming to a browser near you! Don't get scared off by the sea of red for browser support. All browsers have implemented it in their nightly releases, so it's on the way.
How do we use it you say? Simple:
const myObj = {
title: 'Something',
myNestedObj: {
title: 'Something else',
// ... and so on
}
}
const myDeepCopy = structuredClone(myObj);
That's it. You now have a deep clone of your object. There are some limitations to be aware of though.
As always,
Happy coding! 🤓
Cover photo credit
This content originally appeared on DEV Community and was authored by James Thomson

James Thomson | Sciencx (2021-12-16T23:52:02+00:00) Native cloning is coming to Javascript soon!. Retrieved from https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.