minBy
import { minBy } from "jsr:@ende/collections";function
function minBy<T>(array: Iterable<T>, selector: (el: T, index: number) => number): T | undefinedReturns 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) => 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 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 | undefinedReturns 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) => 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 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 | undefinedReturns 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) => 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 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 | undefinedReturns 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) => 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 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