Skip to content

Instantly share code, notes, and snippets.

@antlis
Created April 16, 2026 20:13
Show Gist options
  • Select an option

  • Save antlis/2241314b421f0af71589932352f6ae46 to your computer and use it in GitHub Desktop.

Select an option

Save antlis/2241314b421f0af71589932352f6ae46 to your computer and use it in GitHub Desktop.
Groups array items by a computed key. Lightweight alternative to Lodash groupBy.
export function groupBy(arr, keyFn) {
return arr.reduce((acc, item) => {
const key = keyFn(item);
(acc[key] ||= []).push(item);
return acc;
}, {});
}
@antlis
Copy link
Copy Markdown
Author

antlis commented Apr 16, 2026

Use case:

groupBy(users, u => u.role);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment