Function mapNotNullish

  • Returns a new array, containing all elements in the given array transformed using the given transformer, except the ones that were transformed to null or undefined.

    Type Parameters

    • T

      The type of the elements in the input array.

    • O

      The type of the elements in the output array.

    Parameters

    • array: Iterable<T, any, any>

      The array to map elements from.

    • transformer: ((el: T) => O)

      The function to transform each element.

        • (el): O
        • Parameters

          • el: T

          Returns O

    Returns NonNullable<O>[]

    A new array with all elements transformed by the given transformer, except the ones that were transformed to null or undefined.

    import { mapNotNullish } from "@std/collections/map-not-nullish";
    import { assertEquals } from "@std/assert";

    const people = [
    { middleName: null },
    { middleName: "William" },
    { middleName: undefined },
    { middleName: "Martha" },
    ];
    const foundMiddleNames = mapNotNullish(people, (people) => people.middleName);

    assertEquals(foundMiddleNames, ["William", "Martha"]);