Array method to get distinct items by a selector

This commit is contained in:
Rory&
2026-03-16 23:46:38 +01:00
parent 8c137a42a2
commit 57827bc915
+12
View File
@@ -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;
});
}