
Security News
New React Server Components Vulnerabilities: DoS and Source Code Exposure
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.
group-reduce
Advanced tools
Questions and comments are more than welcome!
Installation is easy if you are using npm:
npm install --save group-reduce
Currently, group-reduce is only available for nodejs:
var group = require('group-reduce');
If you want to use it in a browser, fork this project and export it in the right way. You'll probably want to use an EMCA script polyfill.
First some utility functions, some might not make too much sense until later on:
// Used to reduce an array of numbers to the sum
function add(a, b) { return a + b; }
// Used to map an array of entries to their counts
function getCount(entry) { return entry.count; }
// Used to sort entries by year
function getYear(entry) { return entry.date.getFullYear(); }
We all like a simple example, so here it is:
// Lets say we want to sum the counts per id.
var usage = [
{ id: 1, count: 3 },
{ id: 2, count: 5 },
{ id: 1, count: 2 }
];
/* Reduce the objects which have the same id to the id and the sum. The sum is
* computed by first mapping all entries to the count in the entry, and then
* summing all those numbers. It could be done with only reduce but this is
* easier to read. */
var result = group(usage).by('id').reduce(function(id, entries) {
return {
id: +id,
sum: entries.map(getCount).reduce(add)
};
});
// Result now equals the following:
result = [
{ id: 1, sum: 5 },
{ id: 2, sum: 5 }
];
And for those of you who want a big, commented, in-practice example:
// Compact representation of user usage data sorted first by user and then by date
var compact = [
{ id: 1, usage: [ /* usage object for user 1 */
{ date: '2014-01-01', count: 1 },
{ date: '2014-01-02', count: 2 }
] },
{ id: 1, usage: [ /* another usage object for user 1 */
{ date: '2014-01-03', count: 3 }
] },
{ id: 2, usage: [ /* usage object for user 2 */
{ date: '2014-01-01', count: 1 },
{ date: '2015-01-01', count: 7 }
] }
];
// We expand the user objects into entries for each day in the user object.
var entries = compact.reduce(function(list, user) {
/* Concatenate the array of objects generated from user joined with day
* with the accumulative list. */
return list.concat(user.usage.map(function(day) {
return { id: user.id, date: new Date(day.date), count: day.count };
}));
}, []); // Start the reduction with an empty list.
// Entries now equals the following.
entries = [
{ id: 1, date: new Date('2014-01-01'), count: 1 },
{ id: 1, date: new Date('2014-01-02'), count: 2 },
{ id: 1, date: new Date('2014-01-03'), count: 3 },
{ id: 2, date: new Date('2014-01-01'), count: 1 },
{ id: 2, date: new Date('2015-01-01'), count: 7 }
];
/* Now we would like to group the entries by year and then by user id, this is
* the reverse of what it was initially. */
var results = group(entries)
.by(getYear)
.reduce(function(year, entries) {
/* Here we create and return a date object containing the users which
* have usage in this year. */
return {
date: new Date(+year, 0, 1), // First day of the year.
users: group(entries)
.by('id')
.reduce(function(id, entries) {
/* Here we create and return a user object which has an id
* and a sum property. The sum is the sum of all the
* counts in the entries. */
return {
id: +id,
sum: entries.map(getCount).reduce(add)
};
})
};
});
You use group-reduce like so:
var result = group(entries).and(moreEntries).by(selector).reduce(reductor)
entries and moreEntries can be anything. Non-arrays are put into an array. The function .and(moreEntries) simply internally performs entries.concat(moreEntries).
The function .by(selector) takes the selector argument which is usually a string. For computed keys it can be a function. The result of selector(entry) is used to fill a map with lists of entries.
The function .reduce(reductor) takes a function reductor which is invoked for all keys in the internal map and the entries that belong to that key. So reductor(key, entries). The return value of .reduce(reductor) is a list of all the reducted values.
Alternative to .reduce(reductor) which returns a map instead of an array.
FAQs
Group an array by a (computed) property and reduce it
We found that group-reduce demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.