📈 Returns the maximum value in an array.
If duplicate maximum values are present, the first occurrence is returned.
Syntax
import { max } from '@opentf/std';
max<T>(
arr: T[] = [],
by: (val: T) => number | string = (x: T) => x as unknown as number | string
): T | nullParameters
arr: The source array.by: An optional function to extract the numeric or string value for comparison. Defaults to identity function.
Returns
The maximum value or null if the array is empty (after compacting).
Examples
max([]) //=> null
max([1, undefined, 2, null, 3]) //=> 3
max([1, 2, -3, 4, 5]) //=> 5
max(['a', 'b', 'c']) //=> 'c'
max(['apple', 'mango', 'grapes']) //=> 'mango'
const arr = [
{
name: 'x',
age: 10,
},
{
name: 'y',
age: 16,
},
{
name: 'z',
age: 13,
},
{ name: 'y2', age: 16 },
];
max(arr, (f) => f.age)
//=>
// {
// name: 'y',
// age: 16,
// }