Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Object.groupBy

Object.groupBy() groups an iterable's elements by keys returned from a callback. It returns a null-prototype object whose properties contain arrays of the original elements for each group.

Since
2024

How it works

Object.groupBy() consumes an iterable and invokes the callback for each element with the element and its index. Each callback result becomes a property key, and the returned null-prototype object stores an array of the original elements under every distinct key.

Syntax

Object.groupBy(items, callbackFn)

Parameters

NameTypeDescription
itemsIterableAn iterable, such as an Array, whose elements will be grouped.
callbackFnFunctionA function called for each element; it returns a value coerced to a property key indicating that element's group.

Returns

A null-prototype object whose group properties each contain an array of the associated original elements.

Key points

  • It accepts any iterable, not only arrays, and derives each group from the callback's result.
  • callbackFn is invoked for every element that the iterable provides.
  • The callback may return a string or symbol directly; other values are converted before becoming object keys.
  • The first callback argument is the current element.
  • The second callback argument is the element's index.
  • The result has no Object.prototype and maps each distinct property key to an array.

Common mistakes

  • The result is an object rather than a Map, so access groups through properties such as result.fruit.
  • Non-string and non-symbol callback results are converted to strings, which can merge values that produce the same property key.
  • Mutating a grouped element also changes that same element in the original iterable.

Example: Group inventory items by type

const inventory = [
  { name: "asparagus", type: "vegetables" },
  { name: "bananas", type: "fruit" },
  { name: "goat", type: "meat" },
  { name: "cherries", type: "fruit" },
];

const result = Object.groupBy(inventory, ({ type }) => type);
console.log(result.fruit.map(({ name }) => name)); // ["bananas", "cherries"]

The callback returns each item's type, so the result has one null-prototype property per type containing the matching original elements.

When to use it

Partition records by a string property

You have records with a stable categorical field such as type, status, or department.

Build callback-defined categories

You need to classify iterable values with a computed label.

Create property-keyed groups

You want grouped data represented as ordinary object properties for property-key access.

Interview questions

QWhat does Object.groupBy() return?

It groups an iterable by callback-derived property keys and returns a null-prototype object. Each property contains an array of the elements whose callback result produced that key.

Follow-up: How would you group by an object key? Use Map.groupBy(), because Object.groupBy() converts keys to property keys while Map.groupBy() supports arbitrary values.

QHow is the callback used?

The callback runs once per iterable element and receives the current element and its index. Its return value identifies the group; values that are not strings or symbols are coerced to strings.

Follow-up: What happens to the callback's third argument? The MDN page documents the element and index arguments for Object.groupBy(), so code should not rely on a documented array argument.

QWhen should Object.groupBy() be preferred over Map.groupBy()?

Use Object.groupBy() when group names can be represented by strings or symbols. Use Map.groupBy() when the grouping key must be an arbitrary value, such as an object.

Follow-up: Why can converting an object key to a string be problematic? Different objects can share a string representation, and changes to an object may make that representation stale.

  • Map.groupBy()
  • Object
  • Array.prototype.reduce()

Source: Object.groupBy() | MDN. Checked 2026-10-15.