Given an array of items, group will build up an object where each key is an array of the items that belong in that group.
import*as
import _
_from'radashi'
const
constfish: {
name:string;
source:string;
}[]
fish= [
{
name: string
name: 'Marlin',
source: string
source: 'ocean',
},
{
name: string
name: 'Bass',
source: string
source: 'lake',
},
{
name: string
name: 'Trout',
source: string
source: 'lake',
},
]
const
constfishBySource: {
[x:string]: {
name:string;
source:string;
}[] |undefined;
}
fishBySource=
import _
_.
functiongroup<{
name:string;
source:string;
}, string>(array:readonly {
name:string;
source:string;
}[], getGroupId: (item: {
name:string;
source:string;
}, index:number) =>string): {
[x:string]: {
name:string;
source:string;
}[] |undefined;
}
Categorizes elements from an array into distinct groups. The
function returns an object where each key is a category identifier
determined by the getGroupId function, and each value is an array
containing all elements that belong to that category.