minBy

import { minBy } from "jsr:@ende/collections";

function

function minBy<T>(array: Iterable<T>, selector: (el: T, index: number) => number): T | undefined

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

array: Iterable<T>

The array to find the minimum element in.

selector: (el: T, index: number) => number

The 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 smallest value of the given function or undefined if there are no elements.

Example: Basic usage

import { minBy } from "@ende/collections/min-by";
import { assertEquals } from "@ende/assert";

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

const personWithMinAge = minBy(people, (i) => i.age);

assertEquals(personWithMinAge, { name: "John", age: 23 });

Example: Using the index parameter

import { minBy } from "@ende/collections/min-by";
import { assertEquals } from "@ende/assert";

const array = [4, 3, 2, 1];
const result = minBy(array, (_, index) => index);

assertEquals(result, 4);

function

function minBy<T>(array: Iterable<T>, selector: (el: T, index: number) => string): T | undefined

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

array: Iterable<T>

The array to find the minimum element in.

selector: (el: T, index: number) => string

The 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 smallest value of the given function or undefined if there are no elements.

Example: Basic usage

import { minBy } from "@ende/collections/min-by";
import { assertEquals } from "@ende/assert";

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

const personWithMinName = minBy(people, (person) => person.name);

assertEquals(personWithMinName, { name: "Anna" });

function

function minBy<T>(array: Iterable<T>, selector: (el: T, index: number) => bigint): T | undefined

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

array: Iterable<T>

The array to find the minimum element in.

selector: (el: T, index: number) => bigint

The 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 smallest value of the given function or undefined if there are no elements.

Example: Basic usage

import { minBy } from "@ende/collections/min-by";
import { assertEquals } from "@ende/assert";

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

const personWithMinAge = minBy(people, (i) => i.age);

assertEquals(personWithMinAge, { name: "John", age: 23n });

function

function minBy<T>(array: Iterable<T>, selector: (el: T, index: number) => Date): T | undefined

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

array: Iterable<T>

The array to find the minimum element in.

selector: (el: T, index: number) => Date

The 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 smallest value of the given function or undefined if there are no elements.

Example: Basic usage

import { minBy } from "@ende/collections/min-by";
import { assertEquals } from "@ende/assert";

const people = [
  { name: "Anna", startedAt: new Date("2020-01-01") },
  { name: "Kim", startedAt: new Date("2020-03-01") },
  { name: "John", startedAt: new Date("2019-01-01") },
];

const personWithMinStartedAt = minBy(people, (person) => person.startedAt);

function

function minBy<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

Source