I'm trying to find the max csBasisPerUnit for each csId in my array. In typescript I receive an error on ...csBasisPerUnit in the last line on Math.max(...csBasisPerUnit):
Type 'unknown' must have a 'Symbol.iterator' method that returns an iterator
What do I need to put here or what is it telling me? I don't understand this.
My interface looks like this:
export interface IGroupBy {
csId: number | null,
csBasisPerUnit: number | null
}
Here is the function:
const groupBy = <IGroupBy, K extends keyof IGroupBy>(list: IGroupBy[], getKey: (item: IGroupBy) => K) => {
const reducedArr = list.reduce((previous, currentItem) => {
const group = getKey(currentItem);
if (!previous[group]) previous[group] = [];
previous[group].push(currentItem);
return previous;
}, {} as Record<K, IGroupBy[]>)
const arr = Object.entries(reducedArr);
arr.reduce((c, [k, csBasisPerUnit]) => Object.assign(c, {[k]: Math.max(...csBasisPerUnit)}), {});