Skip to content

Commit 8fb77de

Browse files
committed
docs(uniqBy): update uniqBy docs for selector key support
1 parent 7fc21aa commit 8fb77de

File tree

4 files changed

+110
-39
lines changed

4 files changed

+110
-39
lines changed

docs/ja/reference/array/uniqBy.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# uniqBy
22

3-
変換関数が返す値を基準に、配列から重複した要素を除いた新しい配列を返します
3+
セレクター関数または特定のプロパティキーを基準に重複要素を除いた新しい配列を返します
44

55
```typescript
6-
const uniqueArray = uniqBy(arr, mapper);
6+
const uniqueArray = uniqBy(arr, selector);
77
```
88

99
## 使用法
1010

11-
### `uniqBy(arr, mapper)`
11+
### `uniqBy(arr, selector)`
1212

13-
配列の要素を特定の基準で変換して重複を判断したい場合は `uniqBy` を使用してください。変換関数が同じ値を返す要素のうち最初に現れるものだけを残します。
13+
特定の基準で重複を判定したいときに `uniqBy` を使用してください。セレクターが同じ値を返す要素のうち最初に出現したものだけを残します。セレクターは次のいずれかです。
14+
15+
- 各要素を比較値に変換する関数
16+
- 要素のプロパティキー(例: `'id'`, `'age'`)
1417

1518
```typescript
1619
import { uniqBy } from 'es-toolkit/array';
@@ -20,7 +23,7 @@ const numbers = [1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19];
2023
const result = uniqBy(numbers, Math.floor);
2124
console.log(result); // [1.2, 2.1, 3.2, 5.7, 7.19]
2225

23-
// オブジェクト配列で特定のプロパティを基準に重複を除きます
26+
// セレクター関数で特定のプロパティを基準にします
2427
const users = [
2528
{ id: 1, name: 'john', age: 30 },
2629
{ id: 2, name: 'jane', age: 30 },
@@ -31,13 +34,18 @@ const uniqueByAge = uniqBy(users, user => user.age);
3134
console.log(uniqueByAge);
3235
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
3336

37+
// 同じ処理をプロパティキーだけで書くこともできます。
38+
const uniqueByAgeKey = uniqBy(users, 'age');
39+
console.log(uniqueByAgeKey);
40+
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
41+
3442
// 文字列の長さを基準に重複を除きます。
3543
const words = ['apple', 'pie', 'banana', 'cat', 'dog'];
3644
const uniqueByLength = uniqBy(words, word => word.length);
3745
console.log(uniqueByLength); // ['apple', 'pie', 'banana']
3846
```
3947

40-
複雑なオブジェクトでも特定のフィールドの組み合わせを基準にできます
48+
複雑なオブジェクトでも複数フィールドの組み合わせを基準にできます
4149

4250
```typescript
4351
import { uniqBy } from 'es-toolkit/array';
@@ -49,18 +57,26 @@ const products = [
4957
{ category: 'fruit', name: 'grape' },
5058
];
5159

52-
// カテゴリを基準に重複を除きます。
60+
// カテゴリで重複を除外(セレクター関数)
5361
const uniqueByCategory = uniqBy(products, item => item.category);
5462
console.log(uniqueByCategory.length); // 2
5563
console.log(uniqueByCategory);
5664
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
65+
66+
// プロパティキーだけでも同じ結果になります。
67+
const uniqueByCategoryKey = uniqBy(products, 'category');
68+
console.log(uniqueByCategoryKey.length); // 2
69+
console.log(uniqueByCategoryKey);
70+
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
5771
```
5872

5973
#### パラメータ
6074

61-
- `arr` (`readonly T[]`): 重複を除く配列です。
62-
- `mapper` (`(item: T) => U`): 各要素を比較する値に変換する関数です。
75+
- `arr` (`readonly T[]`): 重複を除外する対象の配列です。
76+
- `selector` (`((item: T) => U) | keyof T`):
77+
- 各要素を比較値へ変換する関数、または
78+
- 重複判定に使うプロパティキーです。
6379

6480
#### 戻り値
6581

66-
(`T[]`): 変換関数の結果を基準に重複が除かれた新しい配列です。元の配列で最初に現れる順序を保持します。
82+
(`T[]`): セレクターの結果を基準に重複を除いた新しい配列です。元の配列で最初に現れる順序を保持します。

docs/ko/reference/array/uniqBy.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
# uniqBy
22

3-
변환 함수가 반환하는 값을 기준으로 배열에서 중복된 요소들을 제거한 새로운 배열을 반환해요.
3+
셀렉터 함수나 특정 프로퍼티 키를 기준으로 중복 요소를 제거한 새로운 배열을 반환해요.
44

55
```typescript
6-
const uniqueArray = uniqBy(arr, mapper);
6+
const uniqueArray = uniqBy(arr, selector);
77
```
88

99
## 사용법
1010

11-
### `uniqBy(arr, mapper)`
11+
### `uniqBy(arr, selector)`
1212

13-
배열의 요소들을 특정 기준으로 변환해서 중복을 판단하고 싶을 때 `uniqBy`를 사용하세요. 변환 함수가 같은 값을 반환하는 요소들 중 처음 나타나는 것만 남겨요.
13+
특정 기준으로 중복을 판단하고 싶을 때 `uniqBy`를 사용하세요. 셀렉터가 같은 값을 반환하는 요소 중 처음 등장한 것만 남겨요. 셀렉터는 다음 둘 중 하나예요.
14+
15+
- 각 요소를 비교 값으로 변환하는 함수
16+
- 요소의 프로퍼티 키(예: `'id'`, `'age'`)
1417

1518
```typescript
1619
import { uniqBy } from 'es-toolkit/array';
1720

18-
// 소수점 숫자들을 내림차순으로 변환해서 중복 제거해요.
21+
// 소수점 숫자들을 내림해서 중복 제거해요.
1922
const numbers = [1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19];
2023
const result = uniqBy(numbers, Math.floor);
2124
console.log(result); // [1.2, 2.1, 3.2, 5.7, 7.19]
2225

23-
// 객체 배열에서 특정 속성을 기준으로 중복 제거해요.
26+
// 셀렉터 함수로 특정 프로퍼티를 기준 삼아요.
2427
const users = [
2528
{ id: 1, name: 'john', age: 30 },
2629
{ id: 2, name: 'jane', age: 30 },
@@ -31,13 +34,18 @@ const uniqueByAge = uniqBy(users, user => user.age);
3134
console.log(uniqueByAge);
3235
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
3336

37+
// 같은 로직을 프로퍼티 키로도 표현할 수 있어요.
38+
const uniqueByAgeKey = uniqBy(users, 'age');
39+
console.log(uniqueByAgeKey);
40+
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
41+
3442
// 문자열 길이를 기준으로 중복 제거해요.
3543
const words = ['apple', 'pie', 'banana', 'cat', 'dog'];
3644
const uniqueByLength = uniqBy(words, word => word.length);
3745
console.log(uniqueByLength); // ['apple', 'pie', 'banana']
3846
```
3947

40-
복잡한 객체에서도 특정 필드의 조합을 기준으로 수 있어요.
48+
복잡한 객체에서도 여러 필드 조합을 기준으로 사용할 수 있어요.
4149

4250
```typescript
4351
import { uniqBy } from 'es-toolkit/array';
@@ -49,18 +57,26 @@ const products = [
4957
{ category: 'fruit', name: 'grape' },
5058
];
5159

52-
// 카테고리를 기준으로 중복 제거해요.
60+
// 카테고리를 기준으로 중복 제거 (셀렉터 함수)
5361
const uniqueByCategory = uniqBy(products, item => item.category);
5462
console.log(uniqueByCategory.length); // 2
5563
console.log(uniqueByCategory);
5664
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
65+
66+
// 프로퍼티 키만으로도 동일하게 사용할 수 있어요.
67+
const uniqueByCategoryKey = uniqBy(products, 'category');
68+
console.log(uniqueByCategoryKey.length); // 2
69+
console.log(uniqueByCategoryKey);
70+
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
5771
```
5872

5973
#### 파라미터
6074

6175
- `arr` (`readonly T[]`): 중복을 제거할 배열이에요.
62-
- `mapper` (`(item: T) => U`): 각 요소를 비교할 값으로 변환하는 함수예요.
76+
- `selector` (`((item: T) => U) | keyof T`):
77+
- 요소를 비교 값으로 변환하는 함수, 또는
78+
- 중복을 판단할 때 사용할 프로퍼티 키예요.
6379

6480
#### 반환 값
6581

66-
(`T[]`): 변환 함수의 결과를 기준으로 중복이 제거된 새로운 배열이에요. 원본 배열에서 처음 나타나는 순서를 유지해요.
82+
(`T[]`): 셀렉터 결과를 기준으로 중복이 제거된 새로운 배열이에요. 원본 배열에서 처음 나타나는 순서를 유지해요.

docs/reference/array/uniqBy.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# uniqBy
22

3-
Returns a new array with duplicate elements removed based on values returned by the transformation function.
3+
Returns a new array with duplicate elements removed based on either:
4+
5+
- values returned by the selector function, or
6+
- the value of a given property key.
47

58
```typescript
6-
const uniqueArray = uniqBy(arr, mapper);
9+
const uniqueArray = uniqBy(arr, selector);
710
```
811

912
## Usage
1013

11-
### `uniqBy(arr, mapper)`
14+
### `uniqBy(arr, selector)`
15+
16+
Use `uniqBy` when you want to determine duplicates by a specific criterion.
17+
It only keeps the first occurrence among elements for which the selector returns the same value.
1218

13-
Use `uniqBy` when you want to transform elements by a specific criterion and determine duplicates. It only keeps the first occurrence among elements for which the transformation function returns the same value.
19+
The selector can be:
20+
21+
- a function that transforms each element into a comparison value, or
22+
- a property key of the elements (e.g. 'id', 'age').
1423

1524
```typescript
1625
import { uniqBy } from 'es-toolkit/array';
@@ -20,7 +29,7 @@ const numbers = [1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19];
2029
const result = uniqBy(numbers, Math.floor);
2130
console.log(result); // [1.2, 2.1, 3.2, 5.7, 7.19]
2231

23-
// Remove duplicates from an object array based on a specific property.
32+
// Remove duplicates from an object array based on a specific property using a selector function.
2433
const users = [
2534
{ id: 1, name: 'john', age: 30 },
2635
{ id: 2, name: 'jane', age: 30 },
@@ -31,6 +40,11 @@ const uniqueByAge = uniqBy(users, user => user.age);
3140
console.log(uniqueByAge);
3241
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
3342

43+
// The same but using a property key.
44+
const uniqueByAgeKey = uniqBy(users, 'age');
45+
console.log(uniqueByAgeKey);
46+
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
47+
3448
// Remove duplicates based on string length.
3549
const words = ['apple', 'pie', 'banana', 'cat', 'dog'];
3650
const uniqueByLength = uniqBy(words, word => word.length);
@@ -49,18 +63,27 @@ const products = [
4963
{ category: 'fruit', name: 'grape' },
5064
];
5165

52-
// Remove duplicates based on category.
66+
// Remove duplicates based on category using a selector function.
5367
const uniqueByCategory = uniqBy(products, item => item.category);
5468
console.log(uniqueByCategory.length); // 2
5569
console.log(uniqueByCategory);
5670
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
71+
72+
// Or using a property key directly.
73+
const uniqueByCategoryKey = uniqBy(products, 'category');
74+
console.log(uniqueByCategoryKey.length); // 2
75+
console.log(uniqueByCategoryKey);
76+
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
5777
```
5878

5979
#### Parameters
6080

6181
- `arr` (`readonly T[]`): The array from which to remove duplicates.
62-
- `mapper` (`(item: T) => U`): A function that transforms each element into a value for comparison.
82+
- `selector` (`((item: T) => U) | keyof T`):
83+
- A function that selects a value for comparison, or
84+
- a property key of `T` used to determine uniqueness.
6385

6486
#### Returns
6587

66-
(`T[]`): A new array with duplicates removed based on the transformation function's results. Preserves the order in which they first appear in the original array.
88+
(`T[]`): A new array with duplicates removed based on the selector’s result.
89+
It preserves the order in which elements first appear in the original array.
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
# uniqBy
22

3-
根据转换函数返回的值,返回去除数组中重复元素后的新数组。
3+
根据选择器函数或指定属性键返回的值,返回去除数组中重复元素后的新数组。
44

55
```typescript
6-
const uniqueArray = uniqBy(arr, mapper);
6+
const uniqueArray = uniqBy(arr, selector);
77
```
88

99
## 用法
1010

11-
### `uniqBy(arr, mapper)`
11+
### `uniqBy(arr, selector)`
1212

13-
当您想根据特定标准转换数组元素来判断重复时,请使用 `uniqBy`。对于转换函数返回相同值的元素,只保留首次出现的元素。
13+
当您想根据特定标准判断重复时,请使用 `uniqBy`。对于选择器得出相同值的元素,只保留首次出现的元素。选择器可以是:
14+
15+
- 将元素转换为比较值的函数
16+
- 元素的属性键(如 `'id'``'age'`
1417

1518
```typescript
1619
import { uniqBy } from 'es-toolkit/array';
1720

18-
// 将小数向下取整转换后去除重复项
21+
// 将小数向下取整后去除重复项
1922
const numbers = [1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19];
2023
const result = uniqBy(numbers, Math.floor);
2124
console.log(result); // [1.2, 2.1, 3.2, 5.7, 7.19]
2225

23-
// 根据对象数组的特定属性去除重复项
26+
// 使用选择器函数按特定属性去重
2427
const users = [
2528
{ id: 1, name: 'john', age: 30 },
2629
{ id: 2, name: 'jane', age: 30 },
@@ -31,13 +34,18 @@ const uniqueByAge = uniqBy(users, user => user.age);
3134
console.log(uniqueByAge);
3235
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
3336

34-
// 根据字符串长度去除重复项。
37+
// 也可以直接传入属性键。
38+
const uniqueByAgeKey = uniqBy(users, 'age');
39+
console.log(uniqueByAgeKey);
40+
// [{ id: 1, name: 'john', age: 30 }, { id: 3, name: 'joe', age: 25 }]
41+
42+
// 根据字符串长度去重。
3543
const words = ['apple', 'pie', 'banana', 'cat', 'dog'];
3644
const uniqueByLength = uniqBy(words, word => word.length);
3745
console.log(uniqueByLength); // ['apple', 'pie', 'banana']
3846
```
3947

40-
对于复杂对象也可以根据特定字段的组合进行去重
48+
对于复杂对象,也可以根据多个字段的组合来判断重复
4149

4250
```typescript
4351
import { uniqBy } from 'es-toolkit/array';
@@ -49,18 +57,26 @@ const products = [
4957
{ category: 'fruit', name: 'grape' },
5058
];
5159

52-
// 根据类别去除重复项。
60+
// 使用选择器函数按类别去重
5361
const uniqueByCategory = uniqBy(products, item => item.category);
5462
console.log(uniqueByCategory.length); // 2
5563
console.log(uniqueByCategory);
5664
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
65+
66+
// 仅用属性键也能实现同样的结果
67+
const uniqueByCategoryKey = uniqBy(products, 'category');
68+
console.log(uniqueByCategoryKey.length); // 2
69+
console.log(uniqueByCategoryKey);
70+
// [{ category: 'fruit', name: 'apple' }, { category: 'vegetable', name: 'carrot' }]
5771
```
5872

5973
#### 参数
6074

6175
- `arr` (`readonly T[]`): 要去除重复项的数组。
62-
- `mapper` (`(item: T) => U`): 将每个元素转换为用于比较的值的函数。
76+
- `selector` (`((item: T) => U) | keyof T`):
77+
- 将元素转换为比较值的函数,或
78+
- 用于判断唯一性的属性键。
6379

6480
#### 返回值
6581

66-
(`T[]`): 根据转换函数的结果去除重复项后的新数组。保持原数组中首次出现的顺序。
82+
(`T[]`): 根据选择器结果去除重复项后的新数组。保持原数组中首次出现的顺序。

0 commit comments

Comments
 (0)