Deep Clone Object
Creates a deep copy of an object, handling nested objects, arrays, and circular references. More robust than JSON.parse/stringify.
JavaScript7/16/2025
#utilities#objects#deep-copy
JavaScript
function deepClone(obj, visited = new WeakMap()) {
if (obj === null || typeof obj !== 'object') return obj;
if (visited.has(obj)) return visited.get(obj);
if (obj instanceof Date) return new Date(obj);
if (obj instanceof Array) {
const copy = [];
visited.set(obj, copy);
obj.forEach((item, index) => {
copy[index] = deepClone(item, visited);
});
return copy;
}
const copy = {};
visited.set(obj, copy);
Object.keys(obj).forEach(key => {
copy[key] = deepClone(obj[key], visited);
});
return copy;
}...
Loading comments...