JavaScript collection of intersection, and, complement, subset of operations implemented

Source: Internet
Author: User
Tags hasownproperty

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.