An object where keys are generated by keyFunc and the values are arrays of elements that share the same key.
A native equivalent, Object.groupBy, is available in ES2024+ (Node.js ≥ 21, all modern browsers).
Prefer the built-in when targeting those environments:
const grouped = Object.groupBy(arr, (item) => item.id);
arrayGroupBy is useful for older targets or when a plain Record<string | number, T[]> return type
is preferred over the Partial<Record<K, T[]>> that Object.groupBy returns.
Groups the elements of an array by a specified key generated from each element.
This function iterates over the provided array and uses a key-generating function (
keyFunc) to determine the key for each element. The result is an object where each key is associated with an array of elements that share the same key.