JavaScript Object.groupBy() Method
The Object.groupBy() static method sorts the items of an array or other iterable into groups, using a function that returns each item's group name.
Description
The Object.groupBy() static method groups the elements of an iterable according to the string returned by a callback. The result is an object with one property per group, each containing an array of the matching elements. It replaces the common pattern of building groups with reduce().
The returned object has no prototype, so it has no inherited methods. If you need keys that aren't strings, such as objects, use Map.groupBy().
The callback receives each item and its index. Items keep their original order within each group, and groups are created in the order their keys are first returned. Because the result is a plain object, use Object.entries() to loop over the groups, or Object.keys() to list them.
Syntax
Object.groupBy(items, callbackFn)Parameters
- itemsAn iterable, such as an array, of the items to group.
- callbackFnA function that returns the group key for each item. It receives
elementandindex.
Return Value
A null-prototype object whose properties are the group keys, each holding an array of items.
Example
Browser Support
The following information will show you the current browser support for the Object.groupBy() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





