FunctionIdentifies the base type of array elements
The array or array-like object to group
Function that determines the group key for each element
OptionalthisArg: anyThe value to use as 'this' when executing callbackFn
An object with group keys as properties and arrays of grouped elements as values
const numbers = [1, 2, 3, 4, 5, 6];
const grouped = arrGroupBy(numbers, (n) => n % 2 === 0 ? "even" : "odd");
// { odd: [1, 3, 5], even: [2, 4, 6] }
const people = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
{ name: "Charlie", age: 30 }
];
const byAge = arrGroupBy(people, (p) => p.age);
// { "25": [{ name: "Bob", age: 25 }], "30": [{ name: "Alice", age: 30 }, { name: "Charlie", age: 30 }] }
The arrGroupBy() method groups array elements by the result of a callback function, returning an object where keys are group identifiers and values are arrays of grouped elements.