Grouping Data with Object.groupBy and Map.groupBy

A
Author
09 Aug, 2026 1 min read 1.49K views

Grouping a list (orders by status, posts by month, people by team) used to need reduce or lodash. Object.groupBy does it in one line.

Object.groupBy

const orders = [
  { id: 1, status: 'paid', total: 500 },
  { id: 2, status: 'pending', total: 250 },
  { id: 3, status: 'paid', total: 1200 },
];

const byStatus = Object.groupBy(orders, (o) => o.status);
// {
//   paid:    [{ id: 1, ... }, { id: 3, ... }],
//   pending: [{ id: 2, ... }]
// }

The callback returns the group key; it can be computed:

const bySize = Object.groupBy(orders, (o) => (o.total >= 1000 ? 'large' : 'small'));

const byMonth = Object.groupBy(posts, (p) => p.publishedAt.slice(0, 7)); // '2026-10'

Map.groupBy for non-string keys

const teams = new Map([[1, 'Platform'], [2, 'Mobile']]);
const people = [{ name: 'Asha', teamId: 1 }, { name: 'Ravi', teamId: 2 }, { name: 'Meera', teamId: 1 }];

const byTeam = Map.groupBy(people, (p) => p.teamId);
byTeam.get(1); // [{ name: 'Asha', ... }, { name: 'Meera', ... }]

Use Map.groupBy when keys are numbers, objects or dates and you want to keep their type.

Summaries from groups

const totals = Object.fromEntries(
  Object.entries(Object.groupBy(orders, (o) => o.status))
    .map(([status, list]) => [status, list.reduce((sum, o) => sum + o.total, 0)])
);
// { paid: 1700, pending: 250 }

Notes

  • The result of Object.groupBy has a null prototype, so it has no inherited methods like hasOwnProperty.
  • It's a static method (Object.groupBy(array, fn)), not array.groupBy.
  • Supported in all modern browsers and Node.js 21+.
Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.