JavaScript introduced two new methods: Map.groupBy
and Object.groupBy
. I understand that both are used to group a list of objects.
According to the documentation on MDN, Map.groupBy
returns its results as a map and is useful when
The method is primarily useful when grouping elements that are associated with an object, and in particular when that object might change over time. If the object is invariant, you might instead represent it using a string, and group elements with Object.groupBy().
and Object.groupBy
returns its results as an object and is useful when
This method should be used when group names can be represented by strings. If you need to group elements using a key that is some arbitrary value, use Map.groupBy() instead.
I don’t understand the reasoning behind these suggestions. Why shouldn’t someone just use Map.groupBy
exclusively?