Array.prototype.group()
Groups array elements into an object keyed by the result of a callback (proposal-stage; superseded by Object.groupBy).
Syntax
arr.group(callback, thisArg) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | Function | Yes | Function called with (element, index, array). Returns a string or symbol key used to group the element. |
thisArg | any | No | Value to use as `this` when executing the callback. |
Returns
object — A null-prototype object whose keys are group names and values are arrays of elements.
Examples
const nums = [1, 2, 3, 4, 5];
const g = nums.group(n => (n % 2 === 0 ? 'even' : 'odd'));
console.log(g);
Output
[Object: null prototype] { odd: [ 1, 3, 5 ], even: [ 2, 4 ] }
// Standardized form (use this instead):
const g = Object.groupBy([1, 2, 3], n => (n > 1 ? 'big' : 'small'));
console.log(g);
Output
[Object: null prototype] { small: [ 1 ], big: [ 2, 3 ] }
Notes
`Array.prototype.group` was a TC39 proposal that did NOT ship as a prototype
method due to web compatibility concerns. It was standardized instead as the
static `Object.groupBy` (and `Map.groupBy`) in ES2024. Prefer `Object.groupBy`;
`arr.group` is unavailable in current engines.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | N/A |
| firefox | N/A |
| safari | N/A |
| edge | N/A |
| node | N/A |