Skip to content

Instantly share code, notes, and snippets.

@alexalannunes
Last active March 29, 2023 11:40
Show Gist options
  • Save alexalannunes/68a27c7b3caa2a48bcb93bf80e2d34b9 to your computer and use it in GitHub Desktop.
Save alexalannunes/68a27c7b3caa2a48bcb93bf80e2d34b9 to your computer and use it in GitHub Desktop.
array intersection, diff, symmetrical difference
const a = [1,2,3]
const b = [1,4,5]
// using find instead includes
const inter = a.filter(x => b.find(x1 => x1 == x));
const diff = a.filter(x => !b.find(x1 => x1 == x));
const simtricDiff = a.filter(x => !b.find(x1 => x1 == x)).concat(b.filter(x => !a.find(x1 => x1 == x)));
inter // [ 1 ]
diff // [ 2, 3 ]
simetricDiff // [ 2, 3, 4, 5 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment