export interface IKeyValues { key: K; values: T[]; } export class ArrayHelper { /** * This method groups given array by specified key, it performs * best with array with sorted key * @param a input array * @param keySelector key selector */ public static groupBy(a: T[], keySelector: (item: T) => K): Array> { const r: Array> = []; let lastKey: any; let lastKeyStore: IKeyValues; for (const iterator of a) { const key = keySelector(iterator); // tslint:disable-next-line:triple-equals if (key != lastKey) { lastKey = key; lastKeyStore = r.find((x) => x.key === key); if (!lastKeyStore) { lastKeyStore = { key, values: [iterator] }; r.push(lastKeyStore); continue; } } lastKeyStore.values.push(iterator); } return r; } }