maxBy
import { maxBy } from "jsr:@ende/collections";function
function maxBy<T>(array: Iterable<T>, selector: (el: T, index: number) => number): T | undefinedReturns the first element that is the largest value of the given function or undefined if there are no elements.
array: Iterable<T>The array to find the maximum element in.
selector: (el: T, index: number) => numberThe function to get the value to compare from each element. The function receives the element and its index.
Returns
The first element that is the largest value of the given function or undefined if there are no elements.
Example: Basic usage
import { maxBy } from "@ende/collections/max-by";
import { assertEquals } from "@ende/assert";
const people = [
{ name: "Anna", age: 34 },
{ name: "Kim", age: 42 },
{ name: "John", age: 23 },
];
const personWithMaxAge = maxBy(people, (person) => person.age);
assertEquals(personWithMaxAge, { name: "Kim", age: 42 });
Example: Using the index parameter
import { maxBy } from "@ende/collections/max-by";
import { assertEquals } from "@ende/assert";
const array = [4, 3, 2, 1];
const result = maxBy(array, (_, index) => index);
assertEquals(result, 1);
function
function maxBy<T>(array: Iterable<T>, selector: (el: T, index: number) => string): T | undefinedReturns the first element that is the largest value of the given function or undefined if there are no elements.
array: Iterable<T>The array to find the maximum element in.
selector: (el: T, index: number) => stringThe function to get the value to compare from each element. The function receives the element and its index.
Returns
The first element that is the largest value of the given function or undefined if there are no elements.
Example: Basic usage
import { maxBy } from "@ende/collections/max-by";
import { assertEquals } from "@ende/assert";
const people = [
{ name: "Anna" },
{ name: "Kim" },
{ name: "John" },
];
const personWithMaxName = maxBy(people, (person) => person.name);
assertEquals(personWithMaxName, { name: "Kim" });
function
function maxBy<T>(array: Iterable<T>, selector: (el: T, index: number) => bigint): T | undefinedReturns the first element that is the largest value of the given function or undefined if there are no elements.
array: Iterable<T>The array to find the maximum element in.
selector: (el: T, index: number) => bigintThe function to get the value to compare from each element. The function receives the element and its index.
Returns
The first element that is the largest value of the given function or undefined if there are no elements.
Example: Basic usage
import { maxBy } from "@ende/collections/max-by";
import { assertEquals } from "@ende/assert";
const people = [
{ name: "Anna", age: 34n },
{ name: "Kim", age: 42n },
{ name: "John", age: 23n },
];
const personWithMaxAge = maxBy(people, (person) => person.age);
assertEquals(personWithMaxAge, { name: "Kim", age: 42n });
function
function maxBy<T>(array: Iterable<T>, selector: (el: T, index: number) => Date): T | undefinedReturns the first element that is the largest value of the given function or undefined if there are no elements.
array: Iterable<T>The array to find the maximum element in.
selector: (el: T, index: number) => DateThe function to get the value to compare from each element. The function receives the element and its index.
Returns
The first element that is the largest value of the given function or undefined if there are no elements.
Example: Basic usage
import { maxBy } from "@ende/collections/max-by";
import { assertEquals } from "@ende/assert";
const people = [
{ name: "Anna", startedAt: new Date("2020-01-01") },
{ name: "Kim", startedAt: new Date("2021-03-01") },
{ name: "John", startedAt: new Date("2020-03-01") },
];
const personWithLastStartedAt = maxBy(people, (person) => person.startedAt);
assertEquals(personWithLastStartedAt, { name: "Kim", startedAt: new Date("2021-03-01") });
function
function maxBy<T>(array: Iterable<T>, selector: ((el: T, index: number) => number) | ((el: T, index: number) => string) | ((el: T, index: number) => bigint) | ((el: T, index: number) => Date)): T | undefined