Function maxWith

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

    The comparator is expected to work exactly like one passed to Array.sort, which means that comparator(a, b) should return a negative number if a < b, a positive number if a > b and 0 if a === b.

    Type Parameters

    • T

      The type of the elements in the array.

    Parameters

    • array: Iterable<T, any, any>

      The array to find the maximum 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 largest value of the given function or undefined if there are no elements.

    import { maxWith } from "@std/collections/max-with";
    import { assertEquals } from "@std/assert";

    const people = ["Kim", "Anna", "John", "Arthur"];
    const largestName = maxWith(people, (a, b) => a.length - b.length);

    assertEquals(largestName, "Arthur");