Skip to content

Instantly share code, notes, and snippets.

@okovalov
Last active July 17, 2020 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okovalov/c770b53ace29fb1df8ac3c349474ddd0 to your computer and use it in GitHub Desktop.
Save okovalov/c770b53ace29fb1df8ac3c349474ddd0 to your computer and use it in GitHub Desktop.
ECMAScript 6 sets: union, intersection, difference
// ECMAScript 6 sets: union, intersection, difference
// 1 union
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let union = new Set([...a, ...b]); // {1,2,3,4}
// 2 intersection
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let intersection = new Set([...a].filter(x => b.has(x))); // {2,3}
// 3 difference
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let difference = new Set([...a].filter(x => !b.has(x))); // {1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment