Identifies the base type of array elements
The current element of the array being processed.
The zero-based index of the current element in the array.
The array (or array-like object) that arrGroupBy was called on.
A string, number, or symbol that identifies the group for the current element.
// Group numbers as "even" or "odd"
const parity: ArrGroupByCallbackFn<number> = (n) => n % 2 === 0 ? "even" : "odd";
arrGroupBy([1, 2, 3, 4, 5], parity);
// { odd: [1, 3, 5], even: [2, 4] }
// Group objects by a property value
interface Person { name: string; dept: string; }
const byDept: ArrGroupByCallbackFn<Person> = (p) => p.dept;
const people: Person[] = [
{ name: "Alice", dept: "eng" },
{ name: "Bob", dept: "hr" },
{ name: "Carol", dept: "eng" }
];
arrGroupBy(people, byDept);
// {
// eng: [{ name: "Alice", dept: "eng" }, { name: "Carol", dept: "eng" }],
// hr: [{ name: "Bob", dept: "hr" }]
// }
Callback function type used by arrGroupBy to derive a group key for each element. The function is called once per element and must return a
string,number, orsymbolthat identifies which group the element belongs to. Elements that map to the same key are collected into the same array in the result object.