1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*!
* Group items from an array together by some criteria or value.
* (c) 2019 Tom Bremmer (https://tbremer.com/) and Chris Ferdinandi (https://gomakethings.com), MIT License,
* @param {Array} arr The array to group items from
* @param {String|Function} criteria The criteria to group by
* @return {Object} The grouped object
*/
export function groupBy<A>(arr: A[], criteria: ((a: A) => string) | string) {
return arr.reduce(function (obj, item) {
// Check if the criteria is a function to run on the item or a property of it
var key = typeof criteria === "function" ? criteria(item) : item[criteria];
// If the key doesn't exist yet, create it
if (!obj.hasOwnProperty(key)) {
obj[key] = [];
}
// Push the value to the object
obj[key].push(item);
// Return the object to the next item in the loop
return obj;
}, {});
}