Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JavaScript now supports 'groupBy`

67 views
Skip to first unread message

Michael Haufe (TNO)

unread,
Aug 21, 2023, 2:42:18 PM8/21/23
to
In case you missed it, JavaScript finally implemented a groupBy static method for Object and Map:

Map.groupBy(iterable, callbackFn)
Object.groupBy(iterable, callbackFn)

Example usage from MDN:

const inventory = [
{ name: "asparagus", type: "vegetables", quantity: 9 },
{ name: "bananas", type: "fruit", quantity: 5 },
{ name: "goat", type: "meat", quantity: 23 },
{ name: "cherries", type: "fruit", quantity: 12 },
{ name: "fish", type: "meat", quantity: 22 },
];

...

const restock = { restock: true };
const sufficient = { restock: false };
const result = Map.groupBy(inventory, ({ quantity }) =>
quantity < 6 ? restock : sufficient,
);
console.log(result.get(restock));
// [{ name: "bananas", type: "fruit", quantity: 5 }]

...

const result = Object.groupBy(inventory, ({ type }) => type);

/* Result is:
{
vegetables: [
{ name: 'asparagus', type: 'vegetables', quantity: 5 },
],
fruit: [
{ name: "bananas", type: "fruit", quantity: 0 },
{ name: "cherries", type: "fruit", quantity: 5 }
],
meat: [
{ name: "goat", type: "meat", quantity: 23 },
{ name: "fish", type: "meat", quantity: 22 }
]
}
*/

More info:

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy>
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy>

Browser Availability:

<https://caniuse.com/?search=groupby>

If you're using the Babel compiler, here is the core-js setting:

<https://github.com/zloirock/core-js#array-grouping>

Julio Di Egidio

unread,
Sep 2, 2023, 5:50:21 AM9/2/23
to
On Monday, 21 August 2023 at 20:42:18 UTC+2, Michael Haufe (TNO) wrote:

> In case you missed it, JavaScript finally implemented
> a groupBy static method for Object and Map

You might have missed that it's neither decently supported,
nor, as for the enthusiasm, it has anything to do with the
aggregation operator by the same name, in fact, with any
aggregation at all: this "groupBy" is more of a "partitionBy"
and can be implemented in one line of code with an array
reduce over the input's keys. Indeed, I'd call it plain clutter.

Julio

luserdroog

unread,
Sep 3, 2023, 9:10:10 AM9/3/23
to
[sad trombone warbling descending glissando w/plunger mute]

Michael Haufe (TNO)

unread,
Sep 3, 2023, 10:56:01 AM9/3/23
to
On Saturday, September 2, 2023 at 4:50:21 AM UTC-5, Julio Di Egidio wrote:
> On Monday, 21 August 2023 at 20:42:18 UTC+2, Michael Haufe (TNO) wrote:
>
> > In case you missed it, JavaScript finally implemented
> > a groupBy static method for Object and Map
> You might have missed that it's neither decently supported,

In case you missed it, I provided availability statistics in my post plus the babel plugin.

> nor, as for the enthusiasm, it has anything to do with the
> aggregation operator by the same name, in fact, with any
> aggregation at all: this "groupBy" is more of a "partitionBy"
> and can be implemented in one line of code with an array
> reduce over the input's keys. Indeed, I'd call it plain clutter.

Given the Universality and Expressiveness of Fold [1], we can say that about most functions/methods

[1] <https://www.cs.nott.ac.uk/~pszgmh/fold.pdf>

Julio Di Egidio

unread,
Sep 3, 2023, 11:19:01 AM9/3/23
to
On Sunday, 3 September 2023 at 16:56:01 UTC+2, Michael Haufe (TNO) wrote:
> On Saturday, September 2, 2023 at 4:50:21 AM UTC-5, Julio Di Egidio wrote:
> > On Monday, 21 August 2023 at 20:42:18 UTC+2, Michael Haufe (TNO) wrote:
> >
> > > In case you missed it, JavaScript finally implemented
> > > a groupBy static method for Object and Map
> >
> > You might have missed that it's neither decently supported,
>
> In case you missed it, I provided availability statistics in my
> post plus the babel plugin.

You provided statistics that apparently you can't read.

> > nor, as for the enthusiasm, it has anything to do with the
> > aggregation operator by the same name, in fact, with any
> > aggregation at all: this "groupBy" is more of a "partitionBy"
> > and can be implemented in one line of code with an array
> > reduce over the input's keys. Indeed, I'd call it plain clutter.
>
> Given the Universality and Expressiveness of Fold [1], we can
> say that about most functions/methods

No, we can't say what I have said of most functions/methods,
but again, you can't even read, apparently.

EOD.

Julio

Arno Welzel

unread,
Sep 3, 2023, 11:39:28 AM9/3/23
to
Michael Haufe (TNO), 2023-08-21 20:42:

> In case you missed it, JavaScript finally implemented a groupBy static method for Object and Map:
>
> Map.groupBy(iterable, callbackFn)
> Object.groupBy(iterable, callbackFn)

Which is not widely supported yet:

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy#browser_compatibility>


--
Arno Welzel
https://arnowelzel.de

Mild Shock

unread,
Sep 3, 2023, 7:43:48 PM9/3/23
to

Omg, the little italian was at it again. How cringe...
What is he, a Bolchevik that is about to kill the Romanov Family?

Julio Di Egidio schrieb:

Michael Haufe (TNO)

unread,
Sep 4, 2023, 9:45:32 AM9/4/23
to
Which I shared already in my OP:

<https://caniuse.com/?search=groupby>

Hence my reference to the polyfill:

<https://github.com/zloirock/core-js#array-grouping>
0 new messages