-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
23 lines (22 loc) · 872 Bytes
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function deepFilter(obj: Record<string, any>, fn: Function): Record<string, any> | undefined {
const dfs = (data: any): any => {
if (Array.isArray(data)) {
const res = data.map(dfs).filter((item: any) => item !== undefined);
return res.length > 0 ? res : undefined;
}
if (typeof data === 'object' && data !== null) {
const res: Record<string, any> = {};
for (const key in data) {
if (data.hasOwnProperty(key)) {
const filteredValue = dfs(data[key]);
if (filteredValue !== undefined) {
res[key] = filteredValue;
}
}
}
return Object.keys(res).length > 0 ? res : undefined;
}
return fn(data) ? data : undefined;
};
return dfs(obj);
}