frontend-challenges
frontend-challenges copied to clipboard
145 - minBy - typescript
index.ts
export function minBy<T>(array: T[], iteratee: (item: T) => number): T | undefined {
if (array.length == 0) return;
return array.reduce((previous, current) => {
const previousValue = iteratee(previous);
const currentValue = iteratee(current);
if (previousValue > currentValue) {
return current;
}
return previous;
});
}