Navigate back to the homepage

How to find maximum value in array in typescript?

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

Hello Friends 👋,

Welcome To Infinitbility! ❤️

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

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


Create Array in typescript

Before going to how to use Math.max() method to get max 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 max value in array

We have to send array element in Math.max() method and it will return highest 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 20.

1// Find largest number from array of numbers
2let ages: Array<number> = [23,46, 36, 96, 3, 30];
3Math.max(...ages);
4
5// ind largest 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.max(...users.map(function(user: User) { return user.age; }));

Output

TypeScript find max Example
TypeScript, find max 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 find minimum value in array in typescript?

TypeScript, find min value in array or array of objects with example.

March 5th, 2022 · 1 min read

How to add new property to object in typescript?

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

February 28th, 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]