mirror of
https://github.com/spacebarchat/server.git
synced 2026-08-28 09:34:07 +00:00
Array method to get distinct items by a selector
This commit is contained in:
@@ -30,3 +30,15 @@ export function arrayRemove<T>(array: T[], item: T): void {
|
||||
array.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
export function arrayDistinctBy<T, M>(array: T[], selector: (elem: T) => M): T[] {
|
||||
const mapped = new Set<M>();
|
||||
|
||||
return array.filter((item) => {
|
||||
const mappedValue = selector(item);
|
||||
if (mapped.has(mappedValue)) return false;
|
||||
|
||||
mapped.add(mappedValue);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user