JavaScript find min/max from array of arrays

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Let's say we have an array of users, but each user is also an array.
For the example, we'll use the following data.
const users = [
['Nicole', 31],
['Chris', 33],
['Yaatree', 2],
['Sanne', 29],
];
As you can see, each user array holds two objects:
How can we quickly return the user with the highest age and the user with the lowest age?
Let's find out together.
First, we need to find a way to extract the data, and I thought the reduce method would be a great one for that.
The trick to using this method is to not pass the initial value. This way, we can only start counting from the actual array of items.
const highest = users.reduce((previous, current) => {
return current[1] > previous[1] ? current : previous;
});
console.log(highest);
// [ 'Chris', 33 ]
Let's see how this works.
As a reminder, the reduce function loop over each array item takes two arguments.
We then check if the current item property 1 (meaning the age) is bigger than the current one.
If that is the case, we return the current item as it's higher. Else, we return whatever was in the previous value.
In our loop, it works like this:
We are ending in Chris being the highest value.
But what if we now take the same approach but want to find a lower age.
We can use the same reduce but swap the arrow.
const lowest = users.reduce((previous, current) => {
return current[1] < previous[1] ? current : previous;
});
console.log(lowest);
// [ 'Yaatree', 2 ]
Note that we switched the > to a <.
Again to explain it more verbally, this is the flow of action:
The result is that we return Yaatree as the lowest.
I've decided to write the reduce function to make it easier for you to read.
However, this is unnecessary, and we can use the shorthand function.
const highest = users.reduce((prev, cur) => (cur[1] > prev[1] ? cur : prev));
const lowest = users.reduce((prev, cur) => (cur[1] < prev[1] ? cur : prev));
This might be a bit harder to read, but we remove the wrapping curly braces, which omit the use of a return statement as they return directly.
But what happens if our array is empty?
We will get the following error: TypeError: Reduce of empty array with no initial value.
That is because we didn't provide an initial value to the reduce method.
We can add an empty null value which would be our fallback.
For the reduce to work, we also need to evaluate starting with the previous value; this way, the other return is always the current.
const users = [];
const highest = users.reduce((previous, current) => {
return previous?.[1] > current[1] ? previous : current;
}, null);
console.log(highest);
// null
I hope you enjoyed this article. Feel free to play around with this in the following CodePen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter