On this page
  1. Map.groupBy
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

Map.groupBy

Prev: has | Up: Map | Next: Set
Rel: Map, Collections Guide
Ref: MDN Web Docs — Map.groupBy
Tags: #public #web #javascript #builtins #map #tier/standard


Signature & Overview

Map.groupBy(items, callbackFn)

Groups elements of an iterable into a Map keyed by callbackFn(item, index) (ES2024).

Notes & Pitfalls

Example

const nums = [1, 2, 3, 4];
const byParity = Map.groupBy(nums, (n) => (n % 2 === 0 ? "even" : "odd"));
console.assert(JSON.stringify(byParity.get("even")) === "[2,4]");

Linked from

Related notes