JavaScript (ES2026) reference · Object Methods

JavaScript Object.groupBy() Method

Group the items of an iterable into an object whose keys come from a callback, e.g. orders grouped by status.

JavaScript (ES2026) Since ES2024

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Object.groupBy(items, callbackFn)

Parameters

ParameterDescription
items An iterable, such as an array.
callbackFn Returns the group key for (item, index).

Return value

A null-prototype object of arrays.

Examples

Group by a fieldTry it yourself
const orders = [{ id: 1, status: "paid" }, { id: 2, status: "open" }, { id: 3, status: "paid" }];
const byStatus = Object.groupBy(orders, (o) => o.status);
console.log(byStatus.paid.map((o) => o.id), byStatus.open.length);

Output

[ 1, 3 ] 1
Group by a computed keyTry it yourself
const nums = [1, 2, 3, 4, 5, 6];
console.log(Object.groupBy(nums, (n) => (n % 2 ? "odd" : "even")));

Output

[Object: null prototype] { odd: [ 1, 3, 5 ], even: [ 2, 4, 6 ] }

Version history

VersionChange
JavaScript ES2024Added

Was this page helpful?