@mirei/ts-collections
    Preparing search index...

    Function maxBy

    • Returns the element whose projected key is greatest according to the provided comparator.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TKey

        Type of key produced by keySelector.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • keySelector: Selector<TElement, TKey>

        Selector used to project each element to the key used for comparison.

      • Optionalcomparator: OrderComparator<TKey, TKey>

        Optional order comparator used to compare keys. Defaults to the library's standard order comparison when omitted.

      Returns TElement

      The element whose key is maximal.

      Thrown when the sequence is empty.

      When multiple elements share the maximal key, the first such element in the sequence is returned.

      const people = [
      { name: 'Alice', age: 25 },
      { name: 'Bob', age: 30 },
      { name: 'Charlie', age: 28 },
      ];
      const oldestPerson = maxBy(people, p => p.age);
      console.log(oldestPerson); // { name: 'Bob', age: 30 }