📉 Returns the minimum value in an array.

Info

If duplicate minimum values are present, the first occurrence is returned.

Syntax

TypeScript
import { min } from '@opentf/std';

min<T>(
  arr: T[] = [],
  by: (val: T) => number | string = (x: T) => x as unknown as number | string
): T | null

Parameters

  • arr: The source array.

  • by: An optional function to extract the numeric or string value for comparison. Defaults to identity function.

Returns

The minimum value or null if the array is empty (after compacting).

Examples

TypeScript
min([]) //=> null

min([1, undefined, 2, null, 3]) //=> 1

min([1, 2, -3, 4, 5]) //=> -3

min(['a', 'b', 'c']) //=> 'a'

min(['apple', 'mango', 'grapes']) //=> 'apple'

const arr = [
  {
    name: 'x',
    age: 10,
  },
  {
    name: 'x2',
    age: 10,
  },
  {
    name: 'y',
    age: 16,
  },
  {
    name: 'z',
    age: 13,
  },
  { name: 'y2', age: 16 },
];
min(arr, (f) => f.age)
//=> 
// {
//  name: 'x',
//  age: 10,
// }
Last updated on
Edit this page