JavaScript Map.groupBy() Method
The Map.groupBy() static method groups the items of an iterable into a Map, using keys of any type returned by a function.
Description
The Map.groupBy() static method works like Object.groupBy(), but the result is a Map, so the group keys can be any value, including objects, rather than just strings.
The callback receives each item and its index, and items keep their original order within each group. Because keys are compared by identity, object keys must be the same object every time; returning a new object literal from the callback would create a separate group for every item.
The result is a normal Map, so you can use get(), has() and for...of on it, or convert it with Object.fromEntries() when the keys are strings.
Syntax
Map.groupBy(items, callbackFn)Parameters
- itemsAn iterable of items to group.
- callbackFnA function returning the group key for each item. Keys can be any type.
Return Value
A Map from each key to an array of the items in that group.
Example
Browser Support
The following information will show you the current browser support for the Map.groupBy() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





