source: node_modules/es-toolkit/dist/array/maxBy.d.mts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/**
2 * Finds the element in an array that has the maximum value when applying
3 * the `getValue` function to each element.
4 *
5 * @template T - The type of elements in the array.
6 * @param {[T, ...T[]]} items The nonempty array of elements to search.
7 * @param {(element: T) => number} getValue A function that selects a numeric value from each element.
8 * @returns {T} The element with the maximum value as determined by the `getValue` function.
9 * @example
10 * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
11 * maxBy([], x => x.a); // Returns: undefined
12 * maxBy(
13 * [
14 * { name: 'john', age: 30 },
15 * { name: 'jane', age: 28 },
16 * { name: 'joe', age: 26 },
17 * ],
18 * x => x.age
19 * ); // Returns: { name: 'john', age: 30 }
20 */
21declare function maxBy<T>(items: readonly [T, ...T[]], getValue: (element: T, index: number, array: readonly T[]) => number): T;
22/**
23 * Finds the element in an array that has the maximum value when applying
24 * the `getValue` function to each element.
25 *
26 * @template T - The type of elements in the array.
27 * @param {T[]} items The array of elements to search.
28 * @param {(element: T, index: number, array: readonly T[]) => number} getValue A function that selects a numeric value from each element.
29 * @returns {T | undefined} The element with the maximum value as determined by the `getValue` function,
30 * or `undefined` if the array is empty.
31 * @example
32 * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
33 * maxBy([], x => x.a); // Returns: undefined
34 * maxBy(
35 * [
36 * { name: 'john', age: 30 },
37 * { name: 'jane', age: 28 },
38 * { name: 'joe', age: 26 },
39 * ],
40 * x => x.age
41 * ); // Returns: { name: 'john', age: 30 }
42 */
43declare function maxBy<T>(items: readonly T[], getValue: (element: T, index: number, array: readonly T[]) => number): T | undefined;
44
45export { maxBy };
Note: See TracBrowser for help on using the repository browser.