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
Top comments (0)