Webapp: Utils: added groupBy function

This commit is contained in:
Francesco 2022-05-01 18:43:52 +02:00
parent 062b6ca99d
commit 130c9dac23
1 changed files with 11 additions and 0 deletions

View File

@ -6,3 +6,14 @@ export function fetchJSON(url: string): Promise<unknown> {
return response.json(); return response.json();
}); });
} }
export function groupBy<V, K>(list: Iterable<V>, mapper: (_el: V) => K): Map<K, V[]> {
const result = new Map();
for (const el of list) {
const key = mapper(el);
if (!result.has(key))
result.set(key, []);
result.get(key).push(el);
}
return result;
}