added Map.groupBy
This commit is contained in:
11
src/util.ts
Normal file
11
src/util.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export default function groupBy<K, V>(arr: V[], fn: (v: V) => K): Map<K, V[]> {
|
||||
const map = new Map<K, V[]>();
|
||||
arr.forEach(e => {
|
||||
const key = fn(e);
|
||||
if (!map.has(key)) {
|
||||
map.set(key, [] as V[]);
|
||||
}
|
||||
map.get(key)!.push(e);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
Reference in New Issue
Block a user