Skip to main content
Recursively filters out empty values from nested objects and arrays. This function removes the following empty values:
  • null and undefined values
  • Empty strings (after trimming whitespace)
  • Empty arrays
  • Empty objects
  • Arrays and objects that become empty after filtering their contents
export declare function filterEmptyValues<T>(input: { data: T }): T;

Examples

import { filterEmptyValues } from "@intuned/browser";
import { BrowserContext, Page } from "playwright";

interface Params {}

export default async function handler(
  params: Params,
  page: Page,
  context: BrowserContext
) {
  // Filter empty values from dictionary
  const result1 = filterEmptyValues({ data: { a: "", b: "hello", c: null } });
  // Output: { b: "hello" }
  console.log(result1);

  // Filter empty values from list
  const result2 = filterEmptyValues({ data: [1, "", null, [2, ""]] });
  // Output: [1, [2]]
  console.log(result2);

  // Filter nested structures
  const result3 = filterEmptyValues({
    data: { users: [{ name: "" }, { name: "John" }] },
  });
  // Output: { users: [{ name: "John" }] }
  console.log(result3);
  return "All data filtered successfully";
}

Arguments

input
Object
required
The input object containing the data to filter

Returns: T

Filtered data structure with empty values removed