maxBy

math

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

Signature

maxBy<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 maximum value

Examples

With objects

import { maxBy } from 'dashlite'

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

Output:

{ n: 2 }

Complex criteria

import { maxBy } from 'dashlite'

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

Output:

{ x: 8 }

Available since version 1.0.0