Skip to content

Instantly share code, notes, and snippets.

@brunos3d
Last active September 20, 2022 14:04
Show Gist options
  • Save brunos3d/e25427a3c5be42b39946c026ef042b8a to your computer and use it in GitHub Desktop.
Save brunos3d/e25427a3c5be42b39946c026ef042b8a to your computer and use it in GitHub Desktop.
arrayComplement and arrayIntersection
export function arrayComplement<T, K1 extends keyof T>(
a: T[],
key: K1,
b: T[K1][]
) {
const setB = new Set(b.map((item) => item));
return a.filter((item) => !setB.has(item[key]));
}
export function arrayIntersection<T, K1 extends keyof T>(
a: T[],
key: K1,
b: T[K1][]
) {
const setB = new Set(b.map((item) => item));
return a.filter((item) => setB.has(item[key]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment