Deep clone utility
📝 JavaScriptCreates a complete independent copy of nested objects and arrays, preventing unintended mutations to the original data.
JavaScript
function deepClone(obj) { if (obj === null || typeof obj !== 'object') return obj; if (Array.isArray(obj)) return obj.map(deepClone); const clone = {}; for (let key in obj) { if (obj.hasOwnProperty(key)) clone[key] = deepClone(obj[key]); } return clone;
}
Comments
Yeah, deep cloning is crucial for avoiding those nasty side effects in state management. I usually reach for structuredClone or JSON.parse(JSON.stringify()) for simple cases.