DEV Community

Discussion on: Learn JavaScript Sets (Simple Yet Powerful Built-In Objects)

Collapse
nqthqn profile image
nqthqn

Computing the difference or intersection between sets can also be helpful. I used this the other day to determine what remaining items I had to find in a computation.

// intersect can be simulated via 
let intersection = new Set([...set1].filter(x => set2.has(x)))

// difference can be simulated via
let difference = new Set([...set1].filter(x => !set2.has(x)))

Compliments of MDN.