JavaScript find min/max from array of objects

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...

In the previous article, we looked at finding the min/max from an array of arrays. Let's see how we can do the same with an array of objects.
Our array will look like this:
const users = [
{ name: 'Nicole', age: 31 },
{ name: 'Chris', age: 33 },
{ name: 'Yaatree', age: 2 },
{ name: 'Sanne', age: 29 },
];
With this array, we want to find the highest and the lowest age users. But not only return the age but the whole object.
Let's see how we can achieve that.
We'll use the reduce method to extract one item from the array. The cool thing about the reduce is that the initial value is optional, so that we can omit it for now.
const highest = users.reduce((previous, current) => {
return current.age > previous.age ? current : previous;
});
console.log('highest', highest);
// { name: 'Chris', age: 33 }
Within this reduce method, we loop over each of our items. For each item, we evaluate if the current item's age property is higher than we previously had. If that is the case, we will return the new one. Else we keep returning the old one.
In our loop, it works like this:
We are ending in Chris being the highest value.
We can use the same approach to find the person with the lowest age.
For that to happen, we must transform the < arrow into a > arrow.
const lowest = users.reduce((previous, current) => {
return current.age < previous.age ? current : previous;
});
console.log('lowest', lowest);
// { name: 'Yaatree', age: 2 }
Again to explain it more verbally, this is the flow of action:
The result is that we return Yaatree as the lowest.
We are very explicit with the above examples by using the curly brackets and returning the object.
However, seeing we only have one line, we can write it as the shorthand function like this.
const highest = users.reduce((prev, cur) => (cur.age > prev.age ? cur : prev));
const lowest = users.reduce((prev, cur) => (cur.age < prev.age ? cur : prev));
Way shorter, right, but it takes some of the readability in one go.
What happens if we pass an empty array, though?
We will get shown the following error: TypeError: Reduce of empty array with no initial value.
This happens because we omitted the initial value of the reduce. To fix that, we can bring back the initial value.
In our case, we would want to set that as null so that it would not return an empty object.
const users = [];
const highest = users.reduce((previous, current) => {
return previous?.age > current.age ? previous : current;
}, null);
console.log('highest', highest);
// null
// Or the shorthand:
const highest = users.reduce(
(prev, cur) => (prev?.age > cur.age ? prev : cur),
null
);
You might also have spotted that for this to work, we had to swap around the previous/current check. This is needed so that the other value is always the current.
I created a CodePen demo where you can have a play with finding the min/max from an array of objects.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter