Favorites

JavaScript Object.groupBy() Method

☕
Enjoying this site?

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

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

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 element and index.

Return Value

A null-prototype object whose properties are the group keys, each holding an array of items.

Example

const people = [
  { name: 'Sam', team: 'red' },
  { name: 'Alex', team: 'blue' },
  { name: 'Lee', team: 'red' },
];
const teams = Object.groupBy(people, p => p.team);
console.log(teams.red.map(p => p.name));

console.log(Object.groupBy([1, 2, 3, 4, 5], n => (n % 2 ? 'odd' : 'even')));

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.

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