Have a array of object and by reduce I transform it to {key: [{}], [{}]}, key is date of array
like: [{date: ’22-02-06-00:55:66′, name: ‘one’}…] and result will be {22-02-06: [{}]} all items what includes to this date. My function is:
export function getObjectOfArrayByKey<T, K extends keyof T>(
data: T[],
key: K,
timeFormat: string,
dateFormat: string,
): { [key in keyof T]: T[] } {
return data.reduce((acc, item) => {
const time = dayjs(item[key]).format(timeFormat);
const date = dayjs(item[key]).format(dateFormat);
const equalToDate = {
time,
...item,
};
acc[date] = acc[date] || [];
acc[date].push(equalToDate);
return acc;
}, {} as { [key in keyof T]: T[] & { time: string } });
}
But in constant equalToDate I add new value “time” and I don’t know how add to type script this new type for time.
I think problem is here, I need add new property for type of new array
{ [key in keyof T]: T[] & { time: string }