JavaScript Object.groupBy() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Object

Definition

Object.groupBy(items, callback) walks an iterable (usually an array) and builds a plain object. For each item, callback returns a key (string or symbol). Matching items are collected into arrays under that key.

This method is relatively new (ES2024). Before using it in production, check typeof Object.groupBy === "function" or provide a fallback.

Syntax

Call Object.groupBy() like this. Start with this line.

JavaScript
Object.groupBy(items, callback)
CallWhat it does
Object.groupBy(items, callback)Groups items into an object of arrays keyed by each callback result.

Built into JavaScript (ES2024). Call on Object — no import required. Relatively new — verify support.

Library declarations
JavaScript
Object.groupBy(items, callbackFn)
JavaScript
groupBy<T>(items: Iterable<T>, callbackFn: (element: T, index: number) => PropertyKey): Partial<Record<PropertyKey, T[]>>;

Parameters

ParameterDescription
itemsRequired. An iterable of values to group, usually an array.
callbackRequired. Called as (element, index). Return a string or symbol used as the group key.

Return Value

Return ValueDescription
objectA null-prototype object whose keys are group names and whose values are arrays of matching items.

JavaScript Examples

Example 1 — Basic Use

Group fruits by color string.

JavaScript
if (typeof Object.groupBy !== "function") {
  console.log("Object.groupBy is not supported here");
} else {
  var fruits = [
    { name: "apple", color: "red" },
    { name: "banana", color: "yellow" },
    { name: "cherry", color: "red" }
  ];

  var byColor = Object.groupBy(fruits, function (item) {
    return item.color;
  });

  console.log(byColor.red.map(function (f) { return f.name; }));
  console.log(byColor.yellow[0].name);
}
Try It Yourself

How It Works

  1. Guard for support Because groupBy is new, the example checks typeof Object.groupBy first.
  2. Return a key The callback returns item.color, which becomes a property name on the result.
  3. Read each group byColor.red is an array of matching objects; yellow has one item.

Example 2 — Even and Odd

Group numbers by a computed label.

JavaScript
if (typeof Object.groupBy !== "function") {
  console.log("Object.groupBy is not supported here");
} else {
  var nums = [1, 2, 3, 4, 5, 6];
  var groups = Object.groupBy(nums, function (n) {
    return n % 2 === 0 ? "even" : "odd";
  });

  console.log(groups.even);
  console.log(groups.odd);
}
Try It Yourself

How It Works

  1. Compute the key The callback does not have to return a property from the item — any string or symbol works.
  2. Two buckets Even numbers land under "even"; odd numbers under "odd".
  3. Order preserved Within each group, items keep the order they had in the original list.

Example 3 — Use the Index

The callback also receives the item’s index.

JavaScript
if (typeof Object.groupBy !== "function") {
  console.log("Object.groupBy is not supported here");
} else {
  var letters = ["a", "b", "c", "d", "e"];
  var byHalf = Object.groupBy(letters, function (letter, index) {
    return index < 3 ? "first" : "rest";
  });

  console.log(byHalf.first);
  console.log(byHalf.rest);
}
Try It Yourself

How It Works

  1. Second argument The callback signature is (element, index).
  2. Split by position Indexes 0–2 go to first; the rest go to rest.
  3. Handy for buckets Use the index when the group depends on position, not only on the value.

Browser & Runtime Support

Object.groupBy() is an ES2024 addition. Support is growing but not universal yet — check typeof Object.groupBy before use.

✓ Newer · ES2024

Object.groupBy()

Available in recent Chrome, Firefox, Safari, and Edge builds. Older browsers need a fallback.

85% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Object.groupBy Good

Bottom line: Confirm support with typeof Object.groupBy === "function". For older engines, group with a loop or use a polyfill.

Frequently Asked Questions

It groups items from an iterable into a plain object. Each callback return value becomes a key, and matching items are stored in an array under that key.
It is relatively new (ES2024). Many modern browsers support it, but you should check typeof Object.groupBy === "function" before relying on it.
Each element and its index: callback(element, index). Return a string or symbol to use as the group key.
Object.groupBy returns a plain object with string/symbol keys. Map.groupBy returns a Map and can use any value as a key.
No. It builds a new object of arrays. The original iterable is left unchanged.

Next: hasOwn()

Check whether a property exists directly on an object.

hasOwn() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful