Function minWith

  • Returns the first element having the smallest value according to the provided comparator or undefined if there are no elements.

    Type Parameters

    • T

      The type of the elements in the array.

    Parameters

    • array: Iterable<T, any, any>

      The array to find the minimum element in.

    • comparator: ((a: T, b: T) => number)

      The function to compare elements.

        • (a, b): number
        • Parameters

          Returns number

    Returns T | undefined

    The first element that is the smallest value of the given function or undefined if there are no elements.

    import { minWith } from "@std/collections/min-with";
    import { assertEquals } from "@std/assert";

    const people = ["Kim", "Anna", "John"];
    const smallestName = minWith(people, (a, b) => a.length - b.length);

    assertEquals(smallestName, "Kim");