Files
twenty/packages/twenty-shared/src/utils/fromArrayToValuesByKeyRecord.util.ts
T
Félix MalfaitandGitHub 8b4b9ef8da Change type import rule (#13751)
Forcing "type" to be explicit, works best will rollup on the frontend to
exclude depdendencies
2025-08-08 01:27:05 +02:00

28 lines
660 B
TypeScript

import { type StringPropertyKeys } from '@/utils/trim-and-remove-duplicated-whitespaces-from-object-string-properties';
import { isDefined } from '@/utils/validation';
export const fromArrayToValuesByKeyRecord = <T extends object>({
array,
key,
}: {
array: T[];
key: StringPropertyKeys<T>;
}) => {
return array.reduce<Record<string, T[]>>((acc, value) => {
const computedKey = value[key] as string;
const occurrence = acc[computedKey];
if (isDefined(occurrence)) {
return {
...acc,
[computedKey]: [...occurrence, value],
};
}
return {
...acc,
[computedKey]: [value],
};
}, {});
};