Skip to content

Instantly share code, notes, and snippets.

@frankiehayward
Created December 5, 2019 23:11
Show Gist options
  • Save frankiehayward/54a7acda32cf3bf7d78ddda118729348 to your computer and use it in GitHub Desktop.
Save frankiehayward/54a7acda32cf3bf7d78ddda118729348 to your computer and use it in GitHub Desktop.
This snippet can be used to return a list of elements that exist in both arrays by using a comparator function.
const intersectionWith = (a, b, comp) => a.filter(x => b.findIndex(y => comp(x, y)) !== -1);
intersectionWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0, 3.9], (a, b) => Math.round(a) === Math.round(b)); // [1.5, 3, 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment