minBy

math

This method is like min except that it accepts iteratee which is invoked for each element in array to generate the criterion by which the value is ranked.

Signature

minBy<T>(array: T[], iteratee: (value: T) => number): T | undefined

Parameters

NameTypeDescription
arrayT[]The array to iterate over
iteratee(value: T) => numberThe iteratee invoked per element

Returns

T | undefined - Returns the minimum value

Examples

With objects

import { minBy } from 'dashlite'

const objects = [{ n: 1 }, { n: 2 }]
const result = minBy(objects, (o) => o.n)
console.log(result)

Output:

{ n: 1 }

Complex criteria

import { minBy } from 'dashlite'

const data = [{ x: 4 }, { x: 2 }, { x: 8 }, { x: 6 }]
const result = minBy(data, (o) => o.x)
console.log(result)

Output:

{ x: 2 }

Available since version 1.0.0