Skip to content

Instantly share code, notes, and snippets.

@ah-cog
Created September 21, 2018 02:47
Show Gist options
  • Save ah-cog/4ae9a95d379abda9f7e2b8999c809413 to your computer and use it in GitHub Desktop.
Save ah-cog/4ae9a95d379abda9f7e2b8999c809413 to your computer and use it in GitHub Desktop.
JavaScript Set Operations

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}

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}

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}

References

  1. MDN JavaScript Reference – Sets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment