Skip to content

2823. Deep Object Filter 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };
type Obj = Record<string, JSONValue> | Array<JSONValue>;

function deepFilter(obj: Obj, fn: Function): Obj | undefined {
  if (obj === null || typeof obj !== 'object') {
    return fn(obj) ? obj : undefined;
  }
  if (Array.isArray(obj)) {
    const ans = (obj as Obj[])
      .map((item) => deepFilter(item, fn))
      .filter((item) => item !== undefined);
    return ans.length > 0 ? ans : undefined;
  }
  let ans = {};
  for (const [key, value] of Object.entries(obj)) {
    const ans = deepFilter(value as Obj, fn);
    if (ans !== undefined) {
      ans[key] = ans;
    }
  }
  return Object.keys(ans).length > 0 ? ans : undefined;
}