Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

JavaScript Array Grouping: How to Group by Property with Object.groupBy and Map.groupBy

JavaScript’s Object.groupBy and Map.groupBy cover most grouping needs, but their different key semantics, null-prototype result, and runtime support matter.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For grouping an array of objects by a property, use Object.groupBy(items, item => item.property) when the group names are property keys; use Map.groupBy(items, callback) when keys may be arbitrary values such as object identities. These built-in static methods now provide the natural comparison point for any custom JavaScript groupBy library. Their design also offers a practical lesson for library authors: a familiar prototype method can create compatibility problems in code already running on the web.

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

Given objects with a type property, Object.groupBy collects items with the same callback result into arrays:

const inventory = [
  { name: "apple", type: "fruit" },
  { name: "pear", type: "fruit" },
  { name: "carrot", type: "vegetable" }
];

const grouped = Object.groupBy(inventory, item => item.type);
console.log(grouped.fruit);
// [{ name: "apple", type: "fruit" }, { name: "pear", type: "fruit" }]

The callback receives each item and its index, and runs once per item. The result is a new grouping container holding arrays of the original item references; the records themselves are not deep-copied. The method accepts an iterable, not only arrays. See MDN’s Object.groupBy reference and the ECMAScript proposal specification.

Choose Object.groupBy or Map.groupBy based on the key

Method Result Key behavior Use it when
Object.groupBy(items, callback) A null-prototype object whose properties hold arrays The callback result is coerced to a property key Groups are naturally addressed by string-like or symbol property keys
Map.groupBy(items, callback) A Map whose values are arrays Keys can be arbitrary values, including object references You need keys that are not property keys, especially object identity

For example, if records refer to owner objects and you want to group by those exact objects:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const byOwner = Map.groupBy(records, record => record.owner);
const ownerRecords = byOwner.get(ownerObject);

Object keys in a Map are matched by identity. Looking up a group with a newly created object that merely has the same properties will not find the group; use the same reference returned by, or passed to, the grouping logic. More detail is in MDN’s Map.groupBy reference.

What Object.groupBy returns—and what to watch for

The object returned by Object.groupBy has no Object.prototype in its prototype chain. It therefore does not inherit methods such as toString or hasOwnProperty. Access known groups directly, or use appropriate object utilities when enumerating or checking properties; do not treat the result as an ordinary object with inherited methods.

This null-prototype result also avoids accidental collisions with inherited object property names. TC39’s proposal describes it this way: “The first returns a null-prototype object, which allows ergonomic destructuring and prevents accidental collisions with global Object properties.” The statement appears in the TC39 Array Grouping proposal.

Why JavaScript uses static grouping methods

It might seem more convenient to call a method on an array, such as items.groupBy(callback). The TC39 proposal explains why the standardized APIs instead use Object.groupBy and Map.groupBy: changing built-in prototypes can collide with behavior already present in deployed libraries and applications.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In particular, the proposal records that older versions of Sugar conditionally installed an incompatible Array.prototype.groupBy. It also notes that Array.prototype.group conflicted with code that used arrays as arbitrary hash maps. The proposal says its investigation found roughly 660 origins using the incompatible Sugar versions; that is a count reported in the proposal’s undated repository text, not a current measurement. Choosing static methods was a way to avoid another web-compatibility hazard. This is a useful constraint for anyone designing a library: API familiarity matters, but so does the behavior of code that already exists.

Are Object.groupBy and Map.groupBy supported in your target runtime?

MDN describes both APIs as widely available across browsers since March 2024. That is broad browser availability, not a guarantee for every older browser or JavaScript runtime. Check the browsers and runtime versions your project actually supports before replacing a dependency or using the methods directly.

  • MDN Object.groupBy lists browser compatibility information; its reference page was last modified July 10, 2025.
  • MDN Map.groupBy provides compatibility details for the Map method.
  • Can I Use’s Object.groupBy table reports 93.25% global usage coverage using StatCounter usage-share data for August 2026. This is a time- and geography-sensitive coverage estimate, not a universal guarantee or a measure of how many developers use the API.

If some supported targets lack the methods, consider a polyfill or retain a custom implementation. The TC39 proposal points to core-js, and MDN’s Object.groupBy reference also lists core-js and es-shims options. Choose based on the exact target matrix and package requirements; the sources cited here do not establish current package versions or runtime-specific release floors.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What a custom groupBy library still needs to decide

Native methods cover the basic grouping operation, but evaluating or designing a library still means checking how its contract fits the application. Compare the output container, accepted keys and coercion rules, whether it accepts general iterables, the callback arguments, handling of special property names and inherited properties, and compatibility with target runtimes. A custom library may also carry a dependency or polyfill cost.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not assume a custom implementation or a native method is faster without comparable, reproducible measurements for your workload. The standards and compatibility sources establish API behavior and the reason for the static design; they do not establish performance rankings.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.