mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-27 14:57:44 +03:00
984a072ff4
* Add example showing how to use uniqBy with object arrays * Add uniqBy example with objects to other docs * Apply suggestions from code review * Update src/array/uniqBy.ts --------- Co-authored-by: Sojin Park <raon0211@gmail.com>
862 B
862 B
uniqBy
Returns a new array containing only the unique elements from the original array, based on the values returned by the mapper
function.
Signature
function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[];
Parameters
arr
(T[]
): The array to process.mapper
((item: T) => U
): The function used to convert the array elements.
Returns
(T[]
): A new array containing only the unique elements from the original array, based on the values returned by the mapper
function.
Examples
uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
// [1.2, 2.1, 3.2, 5.7, 7.19]
const array = [
{ category: 'fruit', name: 'apple' },
{ category: 'fruit', name: 'banana' },
{ category: 'vegetable', name: 'carrot' },
];
uniqBy(array, item => item.category).length
// 2