groupBy es-toolkit
Returns a new object with array elements grouped according to a key-generating function.
Usage
groupBy(arr, getKeyFromItem)
Use groupBy when you want to classify array elements based on specific criteria. Provide a function that generates a key from each element, and it groups elements with the same key together and returns them as an object. The values in the returned object are arrays of elements belonging to each group. This is useful for organizing data by category or performing group-based analysis.
You can group by various criteria.
Parameters
The array to group.
A function that generates a key from each element, its index, and the array.
Returns
Returns an object with elements grouped by key.
Examples
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.