groupByKey
soon we can just use javascript object group by method but until then this method is useful.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function groupByKey<T extends Record<string, any>>(
arr: T[],
key: keyof T,
): Record<string, T[]> {
return arr.reduce(
(acc, item) => {
const groupKey = String(item[key]); // Ensure the key is a string
if (!acc[groupKey]) {
acc[groupKey] = [];
}
acc[groupKey].push(item);
return acc;
},
{} as Record<string, T[]>,
);
}