maxBy
변환 함수가 반환하는 값을 기준으로, 배열에서 최댓값을 가지는 요소를 반환해요.
typescript
const max = maxBy(items, getValue);레퍼런스
maxBy(items, getValue)
배열의 요소들을 변환 함수로 숫자 값으로 바꾸고, 가장 큰 값을 가진 원본 요소를 찾고 싶을 때 maxBy를 사용하세요. 빈 배열에서는 undefined를 반환해요.
typescript
import { maxBy } from 'es-toolkit/array';
// 객체 배열에서 특정 속성의 최댓값을 가진 요소를 찾아요.
const people = [
{ name: 'john', age: 30 },
{ name: 'jane', age: 28 },
{ name: 'joe', age: 26 },
];
maxBy(people, person => person.age);
// Returns: { name: 'john', age: 30 }
// 숫자 배열에서 절댓값이 가장 큰 요소를 찾아요.
const numbers = [-10, -5, 0, 5, 15];
maxBy(numbers, x => Math.abs(x));
// Returns: 15빈 배열에서는 undefined를 반환해요.
typescript
import { maxBy } from 'es-toolkit/array';
maxBy([], x => x.value); // undefined파라미터
items(T[]): 최댓값을 가지는 요소를 찾을 배열이에요.getValue((element: T) => number): 각 요소를 숫자로 변환하는 함수예요.
반환 값
(T | undefined): 변환 함수가 반환한 값 중 가장 큰 값을 가진 요소예요. 배열이 비어 있다면 undefined를 반환해요.

