Map.groupBy(items, callbackFn) walks through items, calls callbackFn(item, index) for each one, and puts that item into an array under the returned key. The result is a new Map. Keys that never appear get no entry. It is a static helper on Map (not called on an instance), newer in the language, and not in every engine yet: check before use.
Remember
Map.groupBy(items, callbackFn) → a new Map
callback returns key "a" → item goes into map.get("a") array
key never returned → no entry for that key
input list → not changed
Syntax
Call Map.groupBy with a list and a function that picks a key for each item.
JavaScript
Map.groupBy(items, callbackFn)
Call
What it does
Map.groupBy(items, callbackFn)
Group every item under the key returned by callbackFn(item, index).
typeof Map.groupBy === "function"
Feature check. Run it before relying on the method.
Static method (ES2024). Call it on Map, not on a Map instance. Each example starts with a short fallback for engines that lack it.
Library declarations
JavaScript
Map.groupBy(items, callbackFn) // returns a new Map
// items: iterable (array, Set, ...)
// callbackFn(item, index): returns the group key
Parameters
Parameter
Description
items
Required. Any iterable of values to group, such as an array.
callbackFn
Required. Called as callbackFn(item, index). Its return value becomes the Map key for that item's group.
Return Value
Return Value
Description
Map
A new Map from each group key to an array of items. The input list is not changed.
JavaScript Examples
Example 1 — Group Numbers by Even or Odd
Split a list of numbers into two groups with a simple key.
JavaScript
// Fills in Map.groupBy only on engines that lack it.
if (!Map.groupBy) {
Map.groupBy = function (items, callbackFn) {
var map = new Map();
var i = 0;
for (var item of items) {
var key = callbackFn(item, i++);
if (!map.has(key)) map.set(key, []);
map.get(key).push(item);
}
return map;
};
}
const nums = [1, 2, 3, 4, 5, 6];
const groups = Map.groupBy(nums, function (n) {
return n % 2 === 0 ? "even" : "odd";
});
console.log(JSON.stringify(groups.get("even"))); // [2,4,6]
console.log(JSON.stringify(groups.get("odd"))); // [1,3,5]
console.log(groups.size); // 2
The property becomes the key. Returning p.team groups everyone with the same team together.
Each value is an array of objects.get("red") holds Ann and Cia; get("blue") holds Bob.
Unused keys are absent.has("green") is false because nobody had that team.
Example 3 — Empty Groups Are Not Created
Only keys that the callback actually returns appear in the Map.
JavaScript
// Fills in Map.groupBy only on engines that lack it.
if (!Map.groupBy) {
Map.groupBy = function (items, callbackFn) {
var map = new Map();
var i = 0;
for (var item of items) {
var key = callbackFn(item, i++);
if (!map.has(key)) map.set(key, []);
map.get(key).push(item);
}
return map;
};
}
const words = ["hi", "hey", "yo"];
const byLen = Map.groupBy(words, function (w) {
return w.length;
});
console.log(JSON.stringify(byLen.get(2))); // ["hi","yo"]
console.log(JSON.stringify(byLen.get(3))); // ["hey"]
console.log(String(byLen.get(4))); // undefined
console.log(byLen.size); // 2