mirror of
https://github.com/toss/es-toolkit.git
synced 2024-12-18 05:21:43 +03:00
docs: Add example showing how to use uniqBy with object arrays (#410)
* 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>
This commit is contained in:
parent
d47603f4cc
commit
984a072ff4
@ -23,3 +23,13 @@ function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[];
|
|||||||
uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
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]
|
// [1.2, 2.1, 3.2, 5.7, 7.19]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const array = [
|
||||||
|
{ category: 'fruit', name: 'apple' },
|
||||||
|
{ category: 'fruit', name: 'banana' },
|
||||||
|
{ category: 'vegetable', name: 'carrot' },
|
||||||
|
];
|
||||||
|
uniqBy(array, item => item.category).length
|
||||||
|
// 2
|
||||||
|
```
|
@ -23,3 +23,14 @@ function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[];
|
|||||||
uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
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]
|
// [1.2, 2.1, 3.2, 5.7, 7.19]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const array = [
|
||||||
|
{ category: 'fruit', name: 'apple' },
|
||||||
|
{ category: 'fruit', name: 'banana' },
|
||||||
|
{ category: 'vegetable', name: 'carrot' },
|
||||||
|
];
|
||||||
|
uniqBy(array, item => item.category).length
|
||||||
|
// 2
|
||||||
|
```
|
||||||
|
|
||||||
|
@ -23,3 +23,13 @@ function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[];
|
|||||||
uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
||||||
// [1.2, 2.1, 3.3, 5.7, 7.19]
|
// [1.2, 2.1, 3.3, 5.7, 7.19]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const array = [
|
||||||
|
{ category: 'fruit', name: 'apple' },
|
||||||
|
{ category: 'fruit', name: 'banana' },
|
||||||
|
{ category: 'vegetable', name: 'carrot' },
|
||||||
|
];
|
||||||
|
uniqBy(array, item => item.category).length
|
||||||
|
// 2
|
||||||
|
```
|
||||||
|
@ -13,6 +13,16 @@
|
|||||||
* uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
* 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]
|
* // [1.2, 2.1, 3.2, 5.7, 7.19]
|
||||||
* ```
|
* ```
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const array = [
|
||||||
|
* { category: 'fruit', name: 'apple' },
|
||||||
|
* { category: 'fruit', name: 'banana' },
|
||||||
|
* { category: 'vegetable', name: 'carrot' },
|
||||||
|
* ];
|
||||||
|
* uniqBy(array, item => item.category).length
|
||||||
|
* // 2
|
||||||
|
* ```
|
||||||
*/
|
*/
|
||||||
export function uniqBy<T, U>(arr: readonly T[], mapper: (item: T) => U): T[] {
|
export function uniqBy<T, U>(arr: readonly T[], mapper: (item: T) => U): T[] {
|
||||||
const map = new Map<U, T>();
|
const map = new Map<U, T>();
|
||||||
|
Loading…
Reference in New Issue
Block a user