These implementations may already be in the new ECMA specification,
But you can start from scratch and feel very good ...
functionSet () {varItems = {}; This. has =function(value) {returnItems.hasownproperty (value); }; This. Add =function(value) {if(! This. has (value)) {Items[value]=value; return true; } return false; }; This. remove =function(value) {if( This. has (value)) { DeleteItems[value]; return true; } return false; }; This. Clear =function() {Items= {}; }; This. Size =function(){ varCount = 0; for(varPropinchitems) { if(Items.hasownproperty (prop)) {++count; } } returncount; }; This. Values =function(){ varKeys = []; for(varKeyinchitems) {Keys.push (key); } returnkeys; }; This. Union =function(otherset) {varUnionset =NewSet (); varValues = This. values (); for(vari=0; i<values.length; i++) {Unionset.add (values[i]); } varValues =otherset.values (); for(vari=0; i<values.length; i++) {Unionset.add (values[i]); } returnUnionset; }; This. intersection =function(otherset) {varIntersectionset =NewSet (); varValues = This. values (); for(vari=0; i<values.length; i++){ if(Otherset.has (Values[i])) {Intersectionset.add (values[i]); } } returnIntersectionset; }; This. Difference =function(otherset) {varDifferenceset =NewSet (); varValues = This. values (); for(vari=0; i<values.length; i++){ if(!Otherset.has (Values[i])) {Differenceset.add (values[i]); } } returnDifferenceset; }; This. subset =function(otherset) {if( This. Size () >otherset.size ()) { return false; } Else { varValues = This. values (); for(vari=0; i<values.length; i++){ if(!Otherset.has (Values[i])) { return false; } } return true; } }}varSet =NewSet (); Set.add (1); Console.log (Set.values ()); //output ["1"]Console.log (Set.has (1));//Output TrueConsole.log (Set.size ());//Output 1Set.add (2); Console.log (Set.values ()); //output ["1", "2"]Console.log (Set.has (2));//trueConsole.log (Set.size ());//2Set.remove (1); Console.log (Set.values ()); //output ["2"]Set.remove (2); Console.log (Set.values ()); //Output []varSetA =NewSet (); Seta.add (1); Seta.add (2); Seta.add (3);varSETB =NewSet (); Setb.add (3); Setb.add (4); Setb.add (5); Setb.add (6);varUnionab =seta.union (SETB); Console.log (Unionab.values ());varSetA =NewSet (); Seta.add (1); Seta.add (2); Seta.add (3);varSETB =NewSet (); Setb.add (2); Setb.add (3); Setb.add (4);varIntersectionab =seta.intersection (SETB); Console.log (Intersectionab.values ());varSetA =NewSet (); Seta.add (1); Seta.add (2); Seta.add (3);varSETB =NewSet (); Setb.add (2); Setb.add (3); Setb.add (4);varDifferenceab =seta.difference (SETB); Console.log (Differenceab.values ());varSetA =NewSet (); Seta.add (1); Seta.add (2);varSETB =NewSet (); Setb.add (1); Setb.add (2); Setb.add (3);varSETC =NewSet (); Setc.add (2); Setc.add (3); Setc.add (4); Console.log (Seta.subset (SETB)); Console.log (Seta.subset (SETC) );
JavaScript collection of intersection, and, complement, subset of operations implemented