Note: In some versions of some browsers, this method was implemented as the method Array.prototype.groupToMap()
. Due to web compatibility issues, it is now implemented as a static method. Check the browser compatibility table for details.
The Map.groupBy()
static method groups the elements of a given iterable using the values returned by a provided callback function. The final returned Map
uses the unique values from the test function as keys, which can be used to get the array of elements in each group.
The method is primarily useful when grouping elements that are associated with an object, and in particular when that object might change over time. If the object is invariant, you might instead represent it using a string, and group elements with Object.groupBy()
.