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
Syntax
Object.groupBy(items, callbackFn)
Parameters
| Parameter | Description |
|---|---|
items |
An iterable, such as an array. |
callbackFn |
Returns the group key for (item, index). |
Return value
A null-prototype object of arrays.
Examples
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
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
| Version | Change |
|---|---|
| JavaScript ES2024 | Added |
Related
Object.fromEntries()· Build an object from an iterable of [key, value] pairs, such as an array or a Map. The opposite of Object.entries().Object.entries()· Return an array of an object's own enumerable [key, value] pairs, ready for for…of loops or array methods.array.reduce()· Reduce the array to a single value by running a reducer function on each element, carrying an accumulator along.- JavaScript Objects · tutorial