JavaScript reduce on multiple properties

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

You might have heard of the JavaScript reduce method, it's used to convert to a specific output, which can be a sum, but also an output array or object.
The reduce itself is super useful, but the other day I needed it to reduce on multiple properties, and I'll show you just how to do this!
For this example we will have the following data set:
const dates = [
{date: '2020-12-08', score: 1},
{date: '2020-12-09', score: 1},
{date: '2020-12-18', score: 1},
{date: '2020-12-22', score: 1},
{date: '2020-12-22', score: 2},
{date: '2020-12-22', score: 3},
{date: '2020-12-22', score: 1},
{date: '2021-01-04', score: 1},
{date: '2021-01-04', score: 2},
{date: '2021-01-04', score: 1},
{date: '2021-01-04', score: 2},
{date: '2021-01-04', score: 3},
{date: '2021-01-04', score: 1},
{date: '2021-01-04', score: 1}
];
As you can see certain dates will be recurring, and they will have specific scores for each item.
The output we want:
An array but with unique dates where the score is the highest!
For just getting unique dates we would actually use a filter, that would make more sense and it would look like this:
dates.filter((value, index, self) => {
return self.findIndex(v => v.date === value.date) === index;
});
This would get unique dates as such:
[
{date: '2020-12-08', score: 1},
{date: '2020-12-09', score: 1},
{date: '2020-12-18', score: 1},
{date: '2020-12-22', score: 1},
{date: '2021-01-04', score: 1}
];
But as you can see it will just take the first item and not the one with the highest score.
To get that we do need to introduce a bit of a bigger query.
dates.reduce((scores, value) => {
let score = scores[value.date];
if (!score) score = value;
else if (score.score < value.score) score = value;
scores[value.date] = score;
return scores;
}, {});
To understand how the reduce works, the scores are the output we pass on, the value is the current loops item.
We set a temporary score variable to store that date's item, then we check if it doesn't exist, we use the score to be this current value.
If it does exist however we need to check if the headache level we already had is smaller, then we update the score variable.
Eventually, we return the scores each time, so it will be a growing object based on date keys.
To get down to earth, the level will be set based on the facts:
The output will look like this:
{
'2020-12-08': { date: '2020-12-08', score: 1 },
'2020-12-09': { date: '2020-12-09', score: 1 },
'2020-12-18': { date: '2020-12-18', score: 1 },
'2020-12-22': { date: '2020-12-22', score: 3 },
'2021-01-04': { date: '2021-01-04', score: 3 }
}
As you can see a bit of a weird object, but we can fix that:
Object.values(
dates.reduce((scores, value) => {
let score = scores[value.date];
if (!score) score = value;
else if (score.score < value.score) score = value;
scores[value.date] = score;
return scores;
}, {})
);
Now we get:
[
{date: '2020-12-08', score: 1},
{date: '2020-12-09', score: 1},
{date: '2020-12-18', score: 1},
{date: '2020-12-22', score: 3},
{date: '2021-01-04', score: 3}
];
Perfect, exactly what we want!
We can then even convert it into a smaller function, but it's arguable that this might not be the most readable.
Object.values(
dates.reduce((scores, value) => {
scores[value.date] = !scores[value.date]
? value
: (scores[value.date] =
scores[value.date].score < value.score ? value : scores[value.date]);
return scores;
}, {})
);
I for one, know what it does since I made this, but looking at this in about two weeks' time will bring big question marks, so I would opt for the above one with more readable if...else statements.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter