Function: useGroupBy()
useGroupBy<
T>(array,key):Record<string,T[]>
Defined in: src/core/services/utils.service.ts:62
Groups array items by a key property or key selector function.
Type Parameters
T
T
Element type.
Parameters
array
T[]
The input array.
key
keyof T | ((item) => string)
A property name or a function that returns the group key.
Returns
Record<string, T[]>
An object mapping group keys to arrays of items.
Example
const items = [
{ type: "fruit", name: "apple" },
{ type: "veggie", name: "carrot" },
{ type: "fruit", name: "banana" },
];
useGroupBy(items, "type");
// { fruit: [{ type: "fruit", name: "apple" }, { type: "fruit", name: "banana" }], veggie: [...] }
useGroupBy(items, (item) => item.type.toUpperCase());
// { FRUIT: [...], VEGGIE: [...] }