Intersection and difference in Sets
suggest changeCurrent JavaScript provides intersection, difference, union, and symmetricDifference on Set. Older engines do not. The same results can be built by filtering arrays:
const set1 = new Set([1, 2, 3, 4]),
set2 = new Set([3, 4, 5, 6]);
const intersection = new Set(Array.from(set1).filter(x => set2.has(x))); // Set {3, 4}
const difference = new Set(Array.from(set1).filter(x => !set2.has(x))); // Set {1, 2}
// Where the methods exist, these are the same sets:
// set1.intersection(set2)
// set1.difference(set2)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents