(items: T[], key: (item: T) => K)
| 1 | /** Groups the given items into a map by their keys */ |
| 2 | export function groupBy<T, K>(items: T[], key: (item: T) => K): Map<K, T[]> { |
| 3 | const map = new Map<K, T[]>(); |
| 4 | for (const item of items) { |
| 5 | const k = key(item); |
| 6 | let ls = map.get(k); |
| 7 | if (ls === undefined) { |
| 8 | ls = []; |
| 9 | map.set(k, ls); |
| 10 | } |
| 11 | ls.push(item); |
| 12 | } |
| 13 | return map; |
| 14 | } |
no test coverage detected