Typescript sum

Date: 2021-11-30
export function sum<T>(items: T[], selector: (item: T) => number) {
    return Array.from(items).reduce((a, x) => a + (selector(x) || 0), 0);
}

interface IWithValue {
    value: number;
}

const data: IWithValue[] = [{value: 1},{value: 2}, {value: 3}, {value: 4}];
console.log(sum(data, (x) => x.value));
// 10
57420cookie-checkTypescript sum