Function maxBy
- max
By <T>(array, selector): T | undefined Type Parameters
Parameters
Returns T | undefined
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 "@std/collections/max-by";
import { assertEquals } from "@std/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 });- max
By <T>(array, selector): T | undefined Returns the first element that is the largest value of the given function or undefined if there are no elements.
Type Parameters
Parameters
Returns T | undefined
The first element that is the largest value of the given function or undefined if there are no elements.
- max
By <T>(array, selector): T | undefined Returns the first element that is the largest value of the given function or undefined if there are no elements.
Type Parameters
Parameters
Returns T | undefined
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 "@std/collections/max-by";
import { assertEquals } from "@std/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 });- max
By <T>(array, selector): T | undefined Returns the first element that is the largest value of the given function or undefined if there are no elements.
Type Parameters
Parameters
Returns T | undefined
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 "@std/collections/max-by";
import { assertEquals } from "@std/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") });
Returns the first element that is the largest value of the given function or undefined if there are no elements.