es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
18 lines (17 loc) • 830 B
TypeScript
/**
* Groups the elements in the array according to the specified key function.
* @param {T[]} array - The array to group.
* @param {Function} fn - The function to determine the grouping key for each element.
* @returns {Record<K, T[]>} An object where keys are the group names and values are arrays of grouped elements.
* @template T, K
* @deprecated - Use Object.groupBy() instead.
* @example
* const people = [
* { name: 'Alice', age: 30 },
* { name: 'Bob', age: 25 },
* { name: 'Charlie', age: 30 }
* ];
* const grouped = groupBy(people, person => person.age);
* // { '25': [{ name: 'Bob', age: 25 }], '30': [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 30 }] }
*/
export declare function groupBy<T extends Record<string, any>, K extends keyof any>(array: T[], fn: (item: T) => K): Record<K, T[]>;