Skip to main content

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Downloads1/wk
Published2 years ago (0.215.0)
function maxBy
maxBy<T>(
array: Iterable<T>,
selector: (el: T) => number
): T | undefined

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

Examples

Example 1

import { maxBy } from "@std/collections/max_by";
import { assertEquals } from "@std/assert/assert_equals";

const people = [
  { name: "Anna", age: 34 },
  { name: "Kim", age: 42 },
  { name: "John", age: 23 },
];

const personWithMaxAge = maxBy(people, (i) => i.age);

assertEquals(personWithMaxAge, { name: "Kim", age: 42 });

Type Parameters

Parameters

array: Iterable<T>
selector: (el: T) => number

Return Type

T | undefined
maxBy<T>(
array: Iterable<T>,
selector: (el: T) => string
): T | undefined

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

Examples

Example 1

import { maxBy } from "@std/collections/max_by";

const people = [
  { name: "Anna" },
  { name: "Kim" },
  { name: "John" },
];

const personWithMaxName = maxBy(people, (i) => i.name);

Type Parameters

Parameters

array: Iterable<T>
selector: (el: T) => string

Return Type

T | undefined
maxBy<T>(
array: Iterable<T>,
selector: (el: T) => bigint
): T | undefined

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

Examples

Example 1

import { maxBy } from "@std/collections/max_by";
import { assertEquals } from "@std/assert/assert_equals";

const people = [
  { name: "Anna", age: 34n },
  { name: "Kim", age: 42n },
  { name: "John", age: 23n },
];

const personWithMaxAge = maxBy(people, (i) => i.age);

assertEquals(personWithMaxAge, { name: "Kim", age: 42n });

Type Parameters

Parameters

array: Iterable<T>
selector: (el: T) => bigint

Return Type

T | undefined
maxBy<T>(
array: Iterable<T>,
selector: (el: T) => Date
): T | undefined

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

Examples

Example 1

import { maxBy } from "@std/collections/max_by";

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, (i) => i.startedAt);

Type Parameters

Parameters

array: Iterable<T>
selector: (el: T) => Date

Return Type

T | undefined

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import { maxBy } from "@std/collections/max_by";
or

Import directly with a jsr specifier

import { maxBy } from "jsr:@std/collections/max_by";