Skip to main content

Function: minByKey()

minByKey<ElementType, MappedType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => MappedType, comparator: CompareByComparator<MappedType>): ElementType | null

Returns the element that gives the minimum value from the specified function.

If several elements are equally minimum, the last element is returned. If the iterator is empty, null is returned.

Type Parameters

Type Parameter
ElementType
MappedType

Parameters

ParameterTypeDefault valueDescription
iterableIterableResolvable<ElementType>undefinedAn iterator of number values to determine the minimum value of.
callbackFn(element: ElementType, index: number) => MappedTypeundefinedA function to execute for each element produced by the iterator. It should return a number value.
comparatorCompareByComparator<MappedType>defaultCompare-

Returns

ElementType | null

The element that gives the minimum value from the specified function, or null if the iterator is empty.

Seealso

min for a version that uses the default comparator.

Seealso

minBy for a version that allows custom comparators.

Example

import { minByKey } from '@sapphire/iterator-utilities';

const iterable = [-3, 0, 1, 5, -10];
console.log(minByKey(iterable, (value) => Math.abs(value)));
// Output: 0

Remarks

This function consumes the entire iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/minByKey.ts:34