Published on August 12, 2024By DeveloperBreeze
JavaScript: How to Deep Clone an Object
// Using JSON methods
const deepClone = (obj) => {
return JSON.parse(JSON.stringify(obj));
};
// Using structured cloning (modern browsers)
const deepCloneModern = structuredClone(obj);
// Usage
const original = { a: 1, b: { c: 2 } };
const clone = deepClone(original);
Comments
Please log in to leave a comment.