2024-06-17 12:39:42 +03:00
|
|
|
# maxBy
|
|
|
|
|
2024-06-28 16:03:54 +03:00
|
|
|
Finds the element in an array that has the maximum value when applying the `getValue` function to each element.
|
2024-06-17 12:39:42 +03:00
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```typescript
|
2024-08-25 16:25:09 +03:00
|
|
|
function maxBy<T>(items: [T, ...T[]], getValue: (element: T) => number): T;
|
|
|
|
function maxBy<T>(items: T[], getValue: (element: T) => number): T | undefined;
|
2024-06-17 12:39:42 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### Parameters
|
|
|
|
|
2024-06-28 16:03:54 +03:00
|
|
|
- `items` (`T[]`): The array of elements to search.
|
|
|
|
- `getValue` (`(item: T) => number`): A function that selects a numeric value from each element.
|
2024-06-17 12:39:42 +03:00
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
2024-07-18 18:00:57 +03:00
|
|
|
(`T`): The element with the maximum value as determined by the `getValue` function.
|
2024-06-17 12:39:42 +03:00
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
```typescript
|
2024-06-19 11:29:50 +03:00
|
|
|
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
|
|
|
|
maxBy([], x => x.a); // Returns: undefined
|
2024-06-17 12:39:42 +03:00
|
|
|
```
|