Skip to content

Instantly share code, notes, and snippets.

@raychew13
Created December 20, 2017 04:43
Show Gist options
  • Save raychew13/62e356b70232287b0d40f10e57c5ea4e to your computer and use it in GitHub Desktop.
Save raychew13/62e356b70232287b0d40f10e57c5ea4e to your computer and use it in GitHub Desktop.
// ------------------- intersection of two arrays ------------------- //
let setA = new Set([1, 2, 3, 4, 8]);
let setB = new Set([4, 3, 2, 10, 7]);
let tmpIntersection = new Set([...setA].filter(x => setB.has(x)));
console.log(tmpIntersection);
// return Set { 2, 3, 4 }
console.log( [...setA].filter(x => setB.has(x)) )
// return [ 2, 3, 4 ]
// ------------------- setA - setB ------------------- //
let setAMinusSetB = new Set([...setA].filter(x => !setB.has(x)));
console.log(setAMinusSetB)
// return Set { 1, 8 }
// ------------------- union of two arrays ------------------- //
var union = [...new Set([...setA, ...setB])];
console.log(union)
// return . [ 1, 2, 3, 4, 8, 10, 7 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment