Last active
July 17, 2020 16:21
-
-
Save okovalov/c770b53ace29fb1df8ac3c349474ddd0 to your computer and use it in GitHub Desktop.
ECMAScript 6 sets: union, intersection, difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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