diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2022-03-09 22:18:14 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-09 22:18:14 +0900 |
| commit | a07037affc38d9f753682dacc4d941838648c74d (patch) | |
| tree | b82de02d43e0489711a727cef015bd76f0ad09a9 /packages/client/src/scripts | |
| parent | Update CHANGELOG.md (diff) | |
| download | sharkey-a07037affc38d9f753682dacc4d941838648c74d.tar.gz sharkey-a07037affc38d9f753682dacc4d941838648c74d.tar.bz2 sharkey-a07037affc38d9f753682dacc4d941838648c74d.zip | |
テーマ選択から重複要素を排除するように (#8385)
Diffstat (limited to 'packages/client/src/scripts')
| -rw-r--r-- | packages/client/src/scripts/array.ts | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/packages/client/src/scripts/array.ts b/packages/client/src/scripts/array.ts index d63f0475d0..29d027de14 100644 --- a/packages/client/src/scripts/array.ts +++ b/packages/client/src/scripts/array.ts @@ -52,6 +52,17 @@ export function unique<T>(xs: T[]): T[] { return [...new Set(xs)]; } +export function uniqueBy<TValue, TKey>(values: TValue[], keySelector: (value: TValue) => TKey): TValue[] { + const map = new Map<TKey, TValue>(); + + for (const value of values) { + const key = keySelector(value); + if (!map.has(key)) map.set(key, value); + } + + return [...map.values()]; +} + export function sum(xs: number[]): number { return xs.reduce((a, b) => a + b, 0); } |