# prefer-group-by

Rule `unicorn/prefer-group-by`. Prefer `Object.groupBy()` or `Map.groupBy()` over manual grouping.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #1066 by reach, #1321 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-group-by` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-group-by.md) |

## Code examples

### Incorrect: the rule reports this

```js
const grouped = items.reduce((groups, item) => {
  groups[item.type] ??= [];
  groups[item.type].push(item);
  return groups;
}, {});
```

```js
const grouped = items.reduce((groups, item) => {
  if (groups.has(item.category)) {
    groups.get(item.category).push(item);
  } else {
    groups.set(item.category, [item]);
  }

  return groups;
}, new Map());
```

```js
const groups = {};
for (const item of items) {
  groups[item.type] ??= [];
  groups[item.type].push(item);
}
```

### Correct: the rule accepts this

```js
const grouped = Object.groupBy(items, item => item.type);
```

```js
const grouped = Map.groupBy(items, item => item.category);
```

```js
const groups = Object.groupBy(items, item => item.type);
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-group-by": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-get-or-insert-computed](https://lintradar.dev/rules/unicorn/prefer-get-or-insert-computed.md): Prefer `.getOrInsertComputed()` when the default value has side effects.
- [unicorn/prefer-hoisting-branch-code](https://lintradar.dev/rules/unicorn/prefer-hoisting-branch-code.md): Prefer moving code shared by all branches of an `if` statement out of the branches.
- [unicorn/prefer-dom-node-html-methods](https://lintradar.dev/rules/unicorn/prefer-dom-node-html-methods.md): Prefer `.getHTML()` and `.setHTML()` over `.innerHTML`.
- [unicorn/prefer-https](https://lintradar.dev/rules/unicorn/prefer-https.md): Prefer HTTPS over HTTP.
- [unicorn/prefer-combined-guards](https://lintradar.dev/rules/unicorn/prefer-combined-guards.md): Prefer combining consecutive guards with identical exit statements.
- [unicorn/prefer-includes-over-repeated-comparisons](https://lintradar.dev/rules/unicorn/prefer-includes-over-repeated-comparisons.md): Prefer `.includes()` over repeated equality comparisons.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-group-by. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
