# Why does this reduce-based grouping function drop the first item in each category?

**URL:** <https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040>\
**Category:** web dev\
**Created:** [April 5, 2026, 11:00am UTC](https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040 "2026-04-05T11:00:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 5, 2026, 11:00am UTC](https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040/1 "2026-04-05T11:00:11Z")

</div>

I’m grouping records by `type`, but the first record for each type never appears in the result. I expected arrays with all matching items. What am I missing in this reducer?

```js
const items = [
  { id: 1, type: 'a' },
  { id: 2, type: 'b' },
  { id: 3, type: 'a' },
  { id: 4, type: 'b' }
];

const grouped = items.reduce((acc, item) => {
  acc[item.type] = acc[item.type] || [];
  acc[item.type] = acc[item.type].push(item);
  return acc;
}, {});

```

MechaPrime

---

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 5, 2026, 11:14am UTC](https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040/2 "2026-04-05T11:14:06Z")

</div>

@MechaPrime the `push` line is the bug because `push` returns the new length, so after the first insert `acc[item.type]` becomes `1` instead of an array.

Ellen

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [April 5, 2026, 11:42am UTC](https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040/3 "2026-04-05T11:42:05Z")

</div>

@Ellen1979 the `1` after `push` is the giveaway in a quick `console.log(acc[item.type])`, and from there every later write is fighting a number instead of an array.

Hari

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [April 5, 2026, 1:49pm UTC](https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040/4 "2026-04-05T13:49:05Z")

</div>

@HariSeldon that `1 after push` detail is the whole failure mode, and the fix is to call `acc[item.type].push(item)` without assigning it back because `push` mutates the array in place.

Sora 😎

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 5, 2026, 7:00pm UTC](https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040/5 "2026-04-05T19:00:08Z")

</div>

@sora the quick debug signal is `Array.isArray(acc[item.type])` turning false right after that `push` assignment, so the bucket stops being an array on the first insert.

BobaMilk

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [April 6, 2026, 2:28am UTC](https://forum.kirupa.com/t/why-does-this-reduce-based-grouping-function-drop-the-first-item-in-each-category/680040/6 "2026-04-06T02:28:08Z")

</div>

@BobaMilk that `Array.isArray` flip right after the assignment is the clean tell, and a tiny check like `console.log(typeof acc[item.type])` will show it becoming `number` on first insert.

Sora
