How to Find Min/Max from Array of Arrays in JavaScript

Updated onbyAlan Morel
How to Find Min/Max from Array of Arrays in JavaScript

Working with an array of arrays is usually rare, but when you do work with, you might find yourself in a situation where you need to find the minimum or maximum value of a specific column in the array.

However, it is not as easy to do this using an array of arrays when compared to a regular array.

In this post, we will see how to find the minimum and maximum value of a specific column in an array of arrays in JavaScript.

How to find the max value of a specific column in an array of arrays

Let's start with a simple array of array:

JAVASCRIPT
const pets = [ ["cat", 10], ["dog", 20], ["bird", 5], ["fish", 1], ["rabbit", 3], ];

This is an array containing 5 arrays.

Each of these arrays contains a pet type and its weight.

Now let's say we want to find pet with the highest weight.

To do this, we can make use of the reduce function.

The reduce function takes a callback function as its first argument, and an initial value as its second argument.

We can then use this to calculate the maximum value of a specific column in the array of arrays.

JAVASCRIPT
const pets = [ ["cat", 10], ["dog", 20], ["bird", 5], ["fish", 1], ["rabbit", 3], ]; const highest = pets.reduce((max, pet) => { return pet[1] > max[1] ? pet : max; }); console.log(highest);
BASH
["dog", 20]

We start off with null as the initial value.

Then we loop through each array in the array of arrays, and compare the value of the second element in the array with the current maximum value.

If the value of the second element is greater than the current maximum value, we update the maximum value to the value of the second element.

Finally, we return the maximum value.

You can write the reduce function in a more concise way by using arrow functions:

JAVASCRIPT
const pets = [ ["cat", 10], ["dog", 20], ["bird", 5], ["fish", 1], ["rabbit", 3], ]; const highest = pets.reduce((max, pet) => (pet[1] > max[1] ? pet : max)); console.log(highest);
BASH
["dog", 20]

How to find the min value of a specific column in an array of arrays

To find the minimum value of a specific column in an array of arrays, we can use the same reduce function.

Let's again use the same array as before:

JAVASCRIPT
const pets = [ ["cat", 10], ["dog", 20], ["bird", 5], ["fish", 1], ["rabbit", 3], ];

Now, let's use the reduce function but this time we will find the lowest weight of all the pets.

JAVASCRIPT
const pets = [ ["cat", 10], ["dog", 20], ["bird", 5], ["fish", 1], ["rabbit", 3], ]; const lowest = pets.reduce((min, pet) => { return pet[1] < min[1] ? pet : min; }); console.log(lowest);
BASH
["fish", 1]

As expected, by just tweaking the comparison operator, we can find the minimum value of a specific column in an array of arrays.

We can use arrow functions to do this more concisely:

JAVASCRIPT
const pets = [ ["cat", 10], ["dog", 20], ["bird", 5], ["fish", 1], ["rabbit", 3], ]; const lowest = pets.reduce((min, pet) => (pet[1] < min[1] ? pet : min)); console.log(lowest);
BASH
["fish", 1]

Conclusion

In this post, we learned how to find the minimum and maximum value of a specific column in an array of arrays in JavaScript.

Simply use the reduce function and provide the proper callback function and initial value.

Thank you for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.