Created
April 16, 2026 20:13
-
-
Save antlis/2241314b421f0af71589932352f6ae46 to your computer and use it in GitHub Desktop.
Groups array items by a computed key. Lightweight alternative to Lodash groupBy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function groupBy(arr, keyFn) { | |
| return arr.reduce((acc, item) => { | |
| const key = keyFn(item); | |
| (acc[key] ||= []).push(item); | |
| return acc; | |
| }, {}); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use case: