How to Find the Maximum Value in an Array of Objects using TypeScript?

In this tutorial, I will explain how to find the maximum value of a specific property in an array of objects using TypeScript. Recently, while working with JSON responses from APIs, I got a similar requirement. I will show you three different approaches to find the max value in a TypeScript array of objects, including using a for loop, the Math.max() method with the spread operator, and the reduce() method.

Find the Maximum Value in an Array of Objects using TypeScript

Let me explain this thing using a real example to help you understand better.

Let’s consider an array of objects representing a list of employees in a US-based company:

interface Employee {
  name: string;
  age: number;
  salary: number;
}

const employees: Employee[] = [
  { name: "John Smith", age: 35, salary: 80000 },
  { name: "Sarah Johnson", age: 28, salary: 95000 },
  { name: "Michael Davis", age: 42, salary: 120000 },
  { name: "Emily Wilson", age: 31, salary: 90000 }
];

Our goal is to find the maximum salary among all the employees. Follow the below methods:

Approach 1: Using a For Loop

The first approach is to use a simple for loop to iterate through the TypeScript array of objects and keep track of the maximum value:

function getMaxSalary(employees: Employee[]): number {
  let maxSalary = 0;
  for (const employee of employees) {
    if (employee.salary > maxSalary) {
      maxSalary = employee.salary;
    }
  }
  return maxSalary;
}

const maxSalary = getMaxSalary(employees);
console.log(`The maximum salary is $${maxSalary}.`);

Output:

The maximum salary is $120000.

This approach is easy to understand. We initialize a variable maxSalary to keep track of the maximum salary. Then, we loop through each employee object, comparing their salary with the current maxSalary. If an employee’s salary is higher, we update maxSalary. Finally, we return the maximum salary found.

Here is the exact output in the screenshot below:

Find the Maximum Value in an Array of Objects using TypeScript

Time Complexity

The time complexity of this approach is O(n), where n is the number of elements in the array. We need to iterate through each element once to find the maximum value.

Check out Sort an Array of Objects by Property Value in TypeScript

Approach 2: Using Math.max() with Map

Another approach is to use the Math.max() method in combination with the map() method to find the max value from an array of objects. The map() method creates a new array with the results of calling a provided function on every element in the array.

Here is an example.

function getMaxSalary(employees: Employee[]): number {
  const salaries = employees.map(employee => employee.salary);
  return Math.max(...salaries);
}

const maxSalary = getMaxSalary(employees);
console.log(`The maximum salary is $${maxSalary}.`);

Output:

The maximum salary is $120000.

In this approach, we first use the map() method to create a new array salaries containing only the salary values from the employee objects. Then, we use the spread operator (...) to pass the salaries array as individual arguments to the Math.max() method, which returns the maximum value among its arguments.

Time Complexity

The time complexity of this approach is also O(n) because we need to iterate through each element once using the map() method to extract the salary values.

Check out Remove Duplicates from an Array of Objects in TypeScript

Approach 3: Using Reduce

The third approach is to use the reduce() method in TypeScript to find the maximum salary in a single iteration:

function getMaxSalary(employees: Employee[]): number {
  return employees.reduce((maxSalary, employee) => 
    Math.max(maxSalary, employee.salary), 0);
}

const maxSalary = getMaxSalary(employees);
console.log(`The maximum salary is $${maxSalary}.`);

Output:

The maximum salary is $120000.

The reduce() method executes a reducer function on each element of the array, resulting in a single output value. In this case, we compare the current maxSalary with the salary of each employee using Math.max(), and the reducer function returns the updated maximum salary. The initial value of maxSalary is set to 0.

Time Complexity

The time complexity of this approach is also O(n) because the reduce() method iterates through each element once to find the maximum value.

Conclusion

In this tutorial, I have explained three different approaches to find the maximum value of a specific property in an array of objects using TypeScript. While all three approaches have the same time complexity of O(n), the reduce() method provides an efficient way to find the maximum value in a single iteration.

I hope this tutorial has helped you understand how to find the maximum value in an array of objects using TypeScript.

You may also like: