memowake-front/components/utils/objectRemoveUmdefined.ts
2025-07-07 13:42:11 +08:00

20 lines
523 B
TypeScript

export function removeUndefined(obj: any): any {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj
.map(removeUndefined)
.filter(item => item !== undefined);
}
const result: Record<string, any> = {};
for (const [key, value] of Object.entries(obj)) {
const newValue = removeUndefined(value);
if (newValue !== undefined) {
result[key] = newValue;
}
}
return result;
}