2024-06-17 12:40:13 +03:00
|
|
|
# minBy
|
|
|
|
|
2024-06-28 16:03:54 +03:00
|
|
|
Finds the element in an array that has the minimum value when applying the `getValue` function to each element.
|
2024-06-17 12:40:13 +03:00
|
|
|
|
|
|
|
If the list is empty, returns `undefined`.
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```typescript
|
2024-06-28 16:03:54 +03:00
|
|
|
function minBy<T>(items: T[], getValue: (item: T) => number): T;
|
2024-06-17 12:40:13 +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:40:13 +03:00
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
2024-06-28 16:03:54 +03:00
|
|
|
The element with the minimum value as determined by the `getValue` function.
|
2024-06-17 12:40:13 +03:00
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
```typescript
|
2024-07-09 14:37:00 +03:00
|
|
|
minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
|
2024-06-19 11:29:50 +03:00
|
|
|
minBy([], x => x.a); // Returns: undefined
|
2024-06-17 12:40:13 +03:00
|
|
|
```
|