Skip to content

Instantly share code, notes, and snippets.

@chip
Forked from clohr/sets.js
Created March 21, 2018 14:47
Show Gist options
  • Save chip/3f3e589956bb3d4694f7e8f7a417fa3e to your computer and use it in GitHub Desktop.
Save chip/3f3e589956bb3d4694f7e8f7a417fa3e to your computer and use it in GitHub Desktop.
ES6 Sets: Intersection, Difference, and Union
const a = new Set([1, 2, 3, 4, 4, 4])
const b = new Set([3, 4, 5, 6])
const intersect = (set1, set2) => [...set1].filter(num => set2.has(num))
const differ = (set1, set2) => [...set1].filter(num => !set2.has(num))
const joinSet = (set1, set2) => [...set1, ...set2]
const myIntersectedSet = new Set(intersect(a, b))
console.log('myIntersectedSet', myIntersectedSet)
const myDifferenceSet = new Set(differ(a, b))
console.log('myDifferenceSet', myDifferenceSet)
const myJoinedSet = new Set(joinSet(a, b))
console.log('myJoinedSet', myJoinedSet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment