20 lines
523 B
TypeScript
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;
|
|
} |