Favorites

JavaScript Map.groupBy() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Map.groupBy()
JavaScript Map Static methodThis method is supported by most modern browsers, though support in some browsers is unknown.
Updated 28th November 2026

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

const stock = [{ name: 'tea', qty: 0 }, { name: 'jam', qty: 5 }, { name: 'oil', qty: 2 }];
const restock = { label: 'restock' }, ok = { label: 'ok' };
const groups = Map.groupBy(stock, i => (i.qty < 3 ? restock : ok));
console.log(groups.get(restock).map(i => i.name));

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.

This method is supported by most modern browsers, though support in some browsers is unknown.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee