diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2022-03-09 23:04:16 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2022-03-09 23:04:16 +0900 |
| commit | 6fc35868ff89c51720aad6e13676d10aa0785cf8 (patch) | |
| tree | fa53821e51401b4c6283c8482452bc52d4f26834 /packages/client/src/scripts/array.ts | |
| parent | Merge branch 'develop' (diff) | |
| parent | 12.108.0 (diff) | |
| download | misskey-6fc35868ff89c51720aad6e13676d10aa0785cf8.tar.gz misskey-6fc35868ff89c51720aad6e13676d10aa0785cf8.tar.bz2 misskey-6fc35868ff89c51720aad6e13676d10aa0785cf8.zip | |
Merge branch 'develop'
Diffstat (limited to 'packages/client/src/scripts/array.ts')
| -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); } |