Function firstNotNullishOf

  • Applies the given selector to elements in the given array until a value is produced that is neither null nor undefined and returns that value. Returns undefined if no such value is produced.

    Type Parameters

    • T

      The type of the elements in the input array.

    • O

      The type of the value produced by the selector function.

    Parameters

    • array: Iterable<T, any, any>

      The array to select a value from.

    • selector: ((item: T) => undefined | null | O)

      The function to extract a value from an element.

        • (item): undefined | null | O
        • Parameters

          • item: T

          Returns undefined | null | O

    Returns NonNullable<O> | undefined

    The first non-null and non-undefined value produced by the selector function, or undefined if no such value is produced.

    import { firstNotNullishOf } from "@std/collections/first-not-nullish-of";
    import { assertEquals } from "@std/assert";

    const tables = [
    { number: 11, order: null },
    { number: 12, order: "Soup" },
    { number: 13, order: "Salad" },
    ];

    const nextOrder = firstNotNullishOf(tables, (table) => table.order);

    assertEquals(nextOrder, "Soup");