Webapp: Utils: added groupBy function
This commit is contained in:
parent
062b6ca99d
commit
130c9dac23
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue