Say I have an array like this:
var arr = [
{type:\"orange\", title:\"First\"},
{type:\"orange\", title:\"Second\"},
{type:\"banana\", title:\"Thi
Typescript version.
/**
* Group object array by property
* Example, groupBy(array, ( x: Props ) => x.id );
* @param array
* @param property
*/
export const groupBy = (array: Array, property: (x: T) => string): { [key: string]: Array } =>
array.reduce((memo: { [key: string]: Array }, x: T) => {
if (!memo[property(x)]) {
memo[property(x)] = [];
}
memo[property(x)].push(x);
return memo;
}, {});
export default groupBy;