| 1 | import { ValueIteratee } from '../_internal/ValueIteratee.js';
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * Finds the element in an array that has the maximum value when applying
|
|---|
| 5 | * the `iteratee` to each element.
|
|---|
| 6 | *
|
|---|
| 7 | * @template T - The type of elements in the array.
|
|---|
| 8 | * @param {ArrayLike<T> | null | undefined} items The array of elements to search.
|
|---|
| 9 | * @param {ValueIteratee<T>} iteratee
|
|---|
| 10 | * The criteria used to determine the maximum value.
|
|---|
| 11 | * - If a **function** is provided, it extracts a numeric value from each element.
|
|---|
| 12 | * - If a **string** is provided, it is treated as a key to extract values from the objects.
|
|---|
| 13 | * - If a **[key, value]** pair is provided, it matches elements with the specified key-value pair.
|
|---|
| 14 | * - If an **object** is provided, it matches elements that contain the specified properties.
|
|---|
| 15 | * @returns {T | undefined} The element with the maximum value as determined by the `iteratee`.
|
|---|
| 16 | * @example
|
|---|
| 17 | * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
|
|---|
| 18 | * maxBy([], x => x.a); // Returns: undefined
|
|---|
| 19 | * maxBy(
|
|---|
| 20 | * [
|
|---|
| 21 | * { name: 'john', age: 30 },
|
|---|
| 22 | * { name: 'jane', age: 28 },
|
|---|
| 23 | * { name: 'joe', age: 26 },
|
|---|
| 24 | * ],
|
|---|
| 25 | * x => x.age
|
|---|
| 26 | * ); // Returns: { name: 'john', age: 30 }
|
|---|
| 27 | * maxBy([{ a: 1 }, { a: 2 }], 'a'); // Returns: { a: 2 }
|
|---|
| 28 | * maxBy([{ a: 1 }, { a: 2 }], ['a', 1]); // Returns: { a: 1 }
|
|---|
| 29 | * maxBy([{ a: 1 }, { a: 2 }], { a: 1 }); // Returns: { a: 1 }
|
|---|
| 30 | */
|
|---|
| 31 | declare function maxBy<T>(items: ArrayLike<T> | null | undefined, iteratee?: ValueIteratee<T>): T | undefined;
|
|---|
| 32 |
|
|---|
| 33 | export { maxBy };
|
|---|