Get The Highest and Lowest Items From a JavaScript Array

Robin
Updated on February 24, 2024

Get The Highest and Lowest Values From an Array of Numbers

You can use a couple of different approaches to get the highest and lowest values from an array of numbers in JavaScript.

The most common and easiest way is by using Math.max() and Math.min() methods.

          const numbers = [4, 10, 5, 3, 8, 2, 6]

const max = Math.max(...numbers)

const min = Math.min(...numbers)

console.log(max) // 10

console.log(min) // 2

        

Call the Math.max() method and spread the numbers array as its argument. This method will return the highest number from the array.

To get the lowest number from an array, call the Math.min() method by spreading the numbers array as its argument.

As you can see, I am getting 10 and 2 from the numbers array because these are the highest and lowest numbers in this array.


Get The Highest and Lowest Items From an Array of Objects

You can easily get the highest and lowest numbers from a number array using the Math object.

But if you have an array of objects and you want to get objects with the highest and lowest value of a specific property, you have to use a different technique.

          const people = [
    { name: 'John', email: 'john@gmail.com', age: 26 },
    { name: 'Bob', email: 'bob@gmail.com', age: 21 },
    { name: 'Max', email: 'max@gmail.com', age: 32 },
    { name: 'Smith', email: 'smith@gmail.com', age: 37 },
    { name: 'Anderson', email: 'anderson@gmail.com', age: 35 },
]
        

Here I have the people array and I want to get objects with the highest and lowest age values.

          const people = [
    { name: 'John', email: 'john@gmail.com', age: 26 },
    { name: 'Bob', email: 'bob@gmail.com', age: 21 },
    { name: 'Max', email: 'max@gmail.com', age: 32 },
    { name: 'Smith', email: 'smith@gmail.com', age: 37 },
    { name: 'Anderson', email: 'anderson@gmail.com', age: 35 },
]

let max = people[0]
let min = people[0]

people.forEach((person) => {
    if (person.age > max.age) {
        max = { ...person }
    }

    if (person.age < min.age) {
        min = { ...person }
    }
})

console.log(max)
// { name: 'Smith', email: 'smith@gmail.com', age: 37 },

console.log(min)
// { name: 'Bob', email: 'bob@gmail.com', age: 21 },

        

First, declare two variables max and min with the first item from the people array.

Second, loop through the people array and check if the age property value of the current item is greater than the age value of the max variable.

If the greater then reassign the max variable with the current person object in the loop.

Third, to get the object with the lowest age, check if the age property value of the current item is lower than the age value of the min variable.

Reassing the min variable with the current person object in the loop, if it is lower.

When I execute the code, I get the "Smith" and "Bob" person objects with the highest and lowest age values.

Related Posts