Create a deep copy of objects and arrays, avoiding reference issues.
export function deepClone<T>(obj: T): T {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime()) as unknown as T;
}
if (obj instanceof Array) {
return obj.map(item => deepClone(item)) as unknown as T;
}
if (typeof obj === 'object') {
const clonedObj = {} as T;
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
clonedObj[key] = deepClone(obj[key]);
}
}
return clonedObj;
}
return obj;
}Creating deep copies of objects is essential when you need to modify data without affecting the original. This utility handles nested objects and arrays.
export function deepClone<T>(obj: T): T {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime()) as unknown as T;
}
if (obj instanceof Array) {
return obj.map(item => deepClone(item)) as unknown as T;
}
if (typeof obj === 'object') {
const clonedObj = {} as T;
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
clonedObj[key] = deepClone(obj[key]);
}
}
return clonedObj;
}
return obj;
}
const original = {
name: 'John',
address: {
city: 'New York',
zip: '10001',
},
hobbies: ['reading', 'coding'],
};
const cloned = deepClone(original);
cloned.address.city = 'Boston'; // Doesn't affect original
cloned.hobbies.push('gaming'); // Doesn't affect original
console.log(original.address.city); // "New York"
console.log(original.hobbies); // ["reading", "coding"]
For simple cases (no functions, Dates, or undefined), you can use:
export function simpleDeepClone<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
Limitations:
undefined valueslodash.cloneDeep or structuredClone (modern browsers).structuredClone is a native browser API that handles more edge cases.A reliable utility for creating independent copies of complex data structures.