How to Find Min/Max from Array of Arrays in JavaScript
Table of Contents
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:
JAVASCRIPTconst 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.
JAVASCRIPTconst 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:
JAVASCRIPTconst 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:
JAVASCRIPTconst 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.
JAVASCRIPTconst 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:
JAVASCRIPTconst 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!
- Getting Started with TypeScript
- Getting Started with Solid
- Getting Started with Svelte
- Create an RSS Reader in Node
- How to build a Discord bot using TypeScript
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- Learn how to use v-model with a custom Vue component
- Learn how to build a Slack Bot using Node.js
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up Stylus CSS Preprocessor