@terminus/ngx-tools
Version:
[![CircleCI][circle-badge]][circle-link] [![codecov][codecov-badge]][codecov-project] [![semantic-release][semantic-release-badge]][semantic-release] [![MIT License][license-image]][license-url] <br> [![NPM version][npm-version-image]][npm-url] [![Github
24 lines (23 loc) • 660 B
TypeScript
/**
* Return an object containing arrays organized by property
*
* @param array - The array to split
* @param key - The object property to split by
* @returns An object containing arrays separated by property value
*
* @example
* interface MyObj {
* a: string;
* b: number;
* }
* const myArray: MyObj[] = [{a: 'foo', b: 1}, {a: 'bar', b: 6}, {a: 'foo', b: 6}];
* groupBy<MyObj, keyof MyObj>(myArray, 'a');
* Returns:
* {
* foo: [{a: 'foo', b: 1}, {a: 'foo', b: 6}],
* bar: [{a: 'bar', b: 6}],
* }
*/
export declare function groupBy<T, K extends (keyof T & (number | string))>(array: T[], key: K): {
[id: string]: T[];
};