-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdistinct.ts
More file actions
34 lines (30 loc) · 1.02 KB
/
Copy pathdistinct.ts
File metadata and controls
34 lines (30 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export {};
declare global {
interface Array<T> {
/**
* [拡張メソッド]
* 配列の重複を除去します。
* @return 重複除去後の配列
*/
distinct(): T[];
/**
* [拡張メソッド]
* 判定対象を比較して配列から重複を除去します。
* @param keySelector 重複判定対象
* @return 重複除去後の配列
*/
distinctBy<K>(keySelector?: (obj: T) => K): T[];
}
}
Array.prototype.distinct = function <T>(): T[] {
const items = this as T[];
if (!Array.isArray(items) || items.length === 0) return [];
return items.filter((item, index, array) => array.indexOf(item) === index);
};
Array.prototype.distinctBy = function <T, K>(keySelector?: (obj: T) => K): T[] {
const items = this as T[];
if (!Array.isArray(items) || items.length === 0) return [];
if (!keySelector) return items.distinct();
const keys = items.map((item) => keySelector(item));
return items.filter((item, index) => keys.indexOf(keySelector(item)) === index);
};