score:1

Accepted answer
const filter = (data: any) => {
  if (Array.isArray(data)) {
    const temp = data.reduce((r, v) => {
      v = filter(v);
      if (v !== "") r.push(v);
      return r;
    }, []);
    return temp.length ? temp : "";
  }
  if (data && typeof data === "object") {
    const temp = Object.entries(data).reduce((r, [k, v]) => {
      v = filter(v);
      if (v !== "") r.push([k, v]);
      return r;
    }, [] as [string, unknown][]);
    return temp.length ? Object.fromEntries(temp) : "";
  }
  return data;
};

Related Query

More Query from same tag