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. Generally, this can be useful to categorize an array.
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 _
_.
group<{
name:string;
source:string;
}, string>(array:readonly {
name:string;
source:string;
}[], getGroupId: (item: {
name:string;
source:string;
}) =>string): {
[x:string]: {
name:string;
source:string;
}[] |undefined;
}
export group
Sorts an array of items into groups. The return value is a map
where the keys are the group IDs the given getGroupId function
produced and the value is an array of each item in that group.