Navigate back to the homepage

How to find minimum value in array in typescript?

Infinitbility
TypeScript
March 5th, 2022 · 1 min read
keep affilitate

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.

1// To make array of numbers in typescript
2let 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.

1// To make array of objects in typescript
2interface User {
3 id: number;
4 name: string;
5 age: number;
6}
7
8let users: Array<User> = [
9 { id: 1, name: 'infinitbility', age: 2 },
10 { id: 2, name: 'notebility', age: 6 },
11 { id: 3, name: 'repairbility', age: 10 },
12];

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.

1// Find lowest number from array of numbers
2let ages: Array<number> = [23,46, 36, 96, 3, 30];
3Math.min(...ages);
4
5// ind lowest number from array of objects
6interface User {
7 id: number;
8 name: string;
9 age: number;
10}
11
12let users: Array<User> = [
13 { id: 1, name: 'infinitbility', age: 2 },
14 { id: 2, name: 'notebility', age: 6 },
15 { id: 3, name: 'repairbility', age: 10 },
16];
17Math.min(...users.map(function(user: User) { return user.age; }));

Output

TypeScript find min Example
TypeScript, find min Example

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to add new property to object in typescript?

TypeScript, add key-value pair in json object example.

February 28th, 2022 · 1 min read

how to find the index of an object in an array in typescript?

TypeScript, find index of object in array example.

February 27th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]