Hello Friends 👋,

Welcome To Infinitbility! ❤️

Today, we will learn and see how to find min value in array or in array of objects. here we will use Math min function to get array min value element.

Let’s start today’s tutorial How to find minimum value in array in typescript?


Create Array in typescript

Before going to how to use Math.min() method to get min value let create some example array of numbers and objects.

To create array of numbers we have to write Array<numbers> before creating, like following example.

// To make array of numbers in typescript
let ages: Array<number> = [23,46, 36, 96, 3, 30];

To create array of objects we have to create interface of object before creating, like following example.

// To make array of objects in typescript
interface User {
  id: number;
  name: string;
  age: number;
}

let users: Array<User> = [
  { id: 1, name: 'infinitbility', age: 2 },
  { id: 2, name: 'notebility', age: 6 },
  { id: 3, name: 'repairbility', age: 10 },
];

Find min value in array

We have to send array element in Math.min() method and it will return lowest number from passed arrays element.

Note: here i’m saying send array of elements not array

we have to pass all our element like this Math.max(2, 6, 7, 20); and then it will return 2.

// Find lowest number from array of numbers
let ages: Array<number> = [23,46, 36, 96, 3, 30];
Math.min(...ages);

// ind lowest number from array of objects
interface User {
  id: number;
  name: string;
  age: number;
}

let users: Array<User> = [
  { id: 1, name: 'infinitbility', age: 2 },
  { id: 2, name: 'notebility', age: 6 },
  { id: 3, name: 'repairbility', age: 10 },
];
Math.min(...users.map(function(user: User) { return user.age; }));

Output

TypeScript find min Example
TypeScript, find min Example

Thanks for reading…