JavaScript Map.groupBy()
Example
// Create an Array
const fruits = [
{name:"apples", quantity:300},
{name:"bananas", quantity:500},
{name:"oranges", quantity:200},
{name:"kiwi", quantity:150}
];
// Callback function to Group Elements
function myCallback({ quantity }) {
return quantity > 200 ? "ok" : "low";
}
// Group by Quantity
const result = Map.groupBy(fruits, myCallback);
Try it Yourself »
Description
The Map.groupBy()
method groups elements of an object
according to string values returned from a callback function.
The Map.groupBy()
method does not change the original object.
Note:
The elements in the original and in the returned object are the same.
Changes will be reflected in both the original and in the returned object.
Object.groupBy() vs Map.groupBy()
The difference between Object.groupBy() and Map.groupBy() is:
Object.groupBy() groups elements into a JavaScript object.
Map.groupBy() groups elements into a Map object.
Syntax
Map.groupBy(iterable, callback)
Parameters
Parameter | Description |
iterable | Required. An iterable array or map. |
callback | Required. A function to execute for each element. The function should return a group name for the element. |
Return Value
Type | Description |
Object | A Map object with the grouped elements. |
Browser Support
Map.groupby()
is an ES2024 feature.
It is fully supported in all modern browsers since March 2024:
Chrome 117 | Edge 117 | Firefox 119 | Safari 17.4 | Opera 103 |
Sep 2023 | Sep 2023 | Oct 2023 | Okt 2024 | May 2023 |