ES5 wording
There are a = [1,2,3,4,5]
var b = [2,4,6,8,10]
// // intersection
var c = a.filter(function(v){ return b.indexOf(v) > -1 })
// // difference set
var d = a.filter(function(v){ return b.indexOf(v) == -1 })
// //Complement
var e = a.filter(function(v){ return !(b.indexOf(v) > -1) })
.concat(b.filter(function(v){ return !(a.indexOf(v) > -1)}))
// union
var f = a.concat(b.filter(function(v){ return !(a.indexOf(v) > -1)}));
console.log ( "Array a:", a);
console.log ( "Array b:", b);
console.log ( "a and the intersection b:", c);
console.log ( "a difference between the set and b:", d);
console.log ( "a and b complement:", e);
console.log ( "a to b and set:", f);
With the wording ES6
A = var [1,2,3,4,5] var B = [2,4,6,8,10] the console.log ( "Array A:", A); the console.log ( "Array b:" , B); var new new SA = the set (A); var the set new new SB = (B); // intersection of the let INTERSECT = a.filter (X => sb.has (X)); // set difference let minus = a.filter (X => sb.has (X)!); // complement let complement = [... a.filter (! x => sb.has (x)), ... b.filter ( = X> sa.has (X))];! // set and the let unionSet = Array.from (the set new new ([... a, ... b])); the console.log ( "a and b intersection: ", INTERSECT); the console.log (" a difference between the set and b: ", minus); the console.log (" complement of a and b: ", complement); the console.log (" a and b union: ", unionSet);