I discovered Object.groupBy(). But why is it attached to Object and not Array. @philna.sh's blog (philna.sh/blog/2023/09...) pointed me to part of the answer. Creating anArray.groupBy() would break sites using various libraries that monkey-patch Array.prototype.groupBy But why not Array.groupBy() ?
Post
After all it takes an Array as input. Re-reading the doc, it's clear why it is attached on Object. It works not just on arrays but on any iterable (developer.mozilla.org/en-US/docs/W...) My second question is, why does it return a (null prototype) object acting as a map instead of map?
developer.mozilla.orgIteration protocols - JavaScript | MDNIteration protocols aren't new built-ins or syntax, but protocols. These protocols can be implemented by any object by following some conventions.
0 reposts0 likes
Turns out there is a Map.groupBy() that returns a Map (developer.mozilla.org/en-US/docs/W...). I really love Map, I wish it where used when mapping things instead of having plain objects everywhere.
developer.mozilla.orgMap.groupBy() - JavaScript | MDNThe Map.groupBy() static method groups the elements of a given iterable using the values returned by a provided callback function. The final returned Map uses the unique values from the test function ...