---
title: "How do I group an array of objects by a property in JavaScript?"
description: "Object.groupBy and Map.groupBy group array items by a key in one call, and are Baseline since 2024. Learn how to use them and the reduce fallback for older targets."
url: https://www.danny.engineering/answer/javascript/how-do-i-group-an-array-of-objects-by-a-property-in-javascript
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Javascript"]
---

# How do I group an array of objects by a property in JavaScript?

## The short answer


Use `Object.groupBy`. Pass it the array and a function that returns the group key for each item. It returns an object with one property per key, each holding an array of the matching items.


```javascript
const orders = [
  { id: 1, status: 'paid' },
  { id: 2, status: 'pending' },
  { id: 3, status: 'paid' },
];

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


## Why it happens


Grouping is one of the most common data tasks: orders by status, posts by tag, events by day. For years JavaScript had no built-in for it, so most code used `reduce` or a utility library. `Object.groupBy` and `Map.groupBy` were added to the language to cover this directly.


They are Baseline as of 2024: supported in Chrome and Edge 117, Firefox 119 and Safari 17.4 and later, and in current Node.js LTS releases. If you support older browsers, use the `reduce` version below or a polyfill.


There are two versions because keys behave differently:

- `Object.groupBy` turns each key into a string property name. Good for string keys, and easy to serialise as JSON.
- `Map.groupBy` keeps keys as they are, so you can group by numbers, booleans, objects or dates without them being converted.

## How to fix it


### Object.groupBy for string keys


The callback receives each item and its index, and can compute a key rather than just read a property.


```javascript
const products = [
  { name: 'Tea', price: 3 },
  { name: 'Cake', price: 12 },
  { name: 'Coffee', price: 4 },
];

const byBand = Object.groupBy(products, (product) =>
  product.price < 5 ? 'budget' : 'premium',
);

console.log(byBand.budget.map((p) => p.name)); // ['Tea', 'Coffee']
```


The returned object has a null prototype, so it does not inherit methods such as `hasOwnProperty`. Use `Object.hasOwn` or `in` to check for a group.


### Map.groupBy for non-string keys


Use it when the key is an object or when you need to keep the key type.


```javascript
const teamA = { name: 'A' };
const teamB = { name: 'B' };

const players = [
  { name: 'Ada', team: teamA },
  { name: 'Grace', team: teamB },
  { name: 'Linus', team: teamA },
];

const byTeam = Map.groupBy(players, (player) => player.team);
console.log(byTeam.get(teamA).length); // 2
```


### reduce for older environments


This produces the same shape as `Object.groupBy` and works everywhere.


```javascript
function groupBy(items, getKey) {
  return items.reduce((groups, item) => {
    const key = getKey(item);
    (groups[key] ??= []).push(item);
    return groups;
  }, {});
}

const byStatus = groupBy(orders, (order) => order.status);
```


### TypeScript


If TypeScript reports that `groupBy` does not exist, your `lib` setting is too old. Add `ES2024` (or a later `ESNext`) to `lib` in `tsconfig.json`.


```json
{
  "compilerOptions": {
    "lib": ["ES2024", "DOM"]
  }
}
```


## Common pitfalls

- Calling it as a method on the array, as in `orders.groupBy(...)`. It is a static method: `Object.groupBy(orders, fn)`.
- Grouping by dates or numbers with `Object.groupBy` and expecting the original values back. Keys become strings; use `Map.groupBy` to keep them.
- Assuming every group exists. Missing keys are simply absent, so `byStatus.refunded` can be `undefined`.
- Mutating items after grouping. Groups hold references to the same objects, not copies.

## Related reading

- [MDN: Object.groupBy()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy)
- [MDN: Map.groupBy()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy)
