# prefer-group-by πŸ“ Prefer `Object.groupBy()` or `Map.groupBy()` over reduce-based grouping. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). [`Object.groupBy()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy) and [`Map.groupBy()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy) express grouping directly and are easier to read than manually grouping with `Array#reduce()`. This rule checks common reduce-based grouping patterns and intentionally ignores uncommon variants instead of trying to interpret every possible reducer. > [!NOTE] > `Object.groupBy()` returns a null-prototype object. Autofixing a reduce that starts from `{}` changes the result's prototype, which only matters for code that depends on inherited object properties or the prototype itself. ## Examples ```js // ❌ const grouped = items.reduce((groups, item) => { groups[item.type] ??= []; groups[item.type].push(item); return groups; }, {}); // βœ… const grouped = Object.groupBy(items, item => item.type); ``` ```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()); // βœ… const grouped = Map.groupBy(items, item => item.category); ```