JS Array intersection and set of difference set de-weight

Source: Internet
Author: User

The most powerful project needs to use the JS array to remove the weight and intersection of some operations, my array of elements may reach more than 1000, the implementation of the network is 2 cycles, the performance is not suitable for my needs, 1000*1000 that cycle too many times, so I am here to use objects object to do processing, with space to change the time , code is as follows:

  ///Collection IntersectionArray.intersect =function () {varresult =NewArray (); varArrcount = Arguments.length;/*number of arrays, default of 2 array intersection*/            if(Arrcount <=1) {return; } varType =typeof(arguments[0][0]);  for(varK =1; K < Arrcount; k++) {                if(Arguments[k].length <1) {                    return;//Each array must have a value                }                if(Type! =typeof(arguments[k][0])) {//each array element is of the same type                    return; }            }            varobj = {};  for(vari =0; i < Arrcount; i++) {                vararr =Arguments[i];  for(varj =0; J < Arr.length; J + +) {                    if(!Obj[arr[j]]) {Obj[arr[j]]=1; }                    Else{Obj[arr[j]]++; }                }            }             for(varNuminchobj) {                if(Obj[num] = =Arrcount) {                    if(Type = =typeof(0) {result.push (num-0); }                    Else{result.push (num); }                }            }            returnresult; } Array.prototype.intersect=function (arr) {Array.intersect ( This, arr); }        //set to remove duplicatesArray.prototype.uniquelize =function () {varTMP ={}, ret= [];  for(vari =0, j = This. length; I < J; i++) {                if(!tmp[ This[i]]) {tmp[ This[i]] =1; Ret.push ( This[i]); }            }            returnret; }        //and setArray.union =function () {varArrcount = Arguments.length;/*number of arrays, default of 2 array intersection*/            if(Arrcount <=1) {return; } varType =typeof(arguments[0][0]);  for(varK =1; K < Arrcount; k++) {                if(Arguments[k].length <1) {                    return;//Each array must have a value                }                if(Type! =typeof(arguments[k][0])) {//each array element is of the same type                    return; }            }            vartemp = arguments[0]; varArrcount =temp.length;  for(vari =1; i < arguments.length; i++) {                vararr =Arguments[i]; if(I >1) {Arrcount+=arr.length; }                 for(varj =0; J < Arr.length; J + +) {Temp[arrcount+ j] =Arr[j]; }} Temp= Temp.uniquelize ();//Remove Duplicates            returntemp; } Array.prototype.union=function (AR) {array.union ( This, AR); }        //the difference set of 2 sets does not exist in ArrArray.prototype.minus =function (arr) {varresult =NewArray (); varobj = {}; varType =typeof( This[0]);  for(vari =0; I < This. length; i++) {obj[ This[i]] =true; }             for(vari =0; i < arr.length; i++) {                if(Obj[arr[i]]) {Obj[arr[i]]=false; }            }             for(varNuminchobj) {                if(Obj[num]) {if(Type = =typeof(0) {result.push (num-0); }                    Else{result.push (num); }                }            }            returnresult;        }; Console.log (Array.intersect (["1","2","3"], ["2","3","4","5","6"]));//[2,3]Console.log ([1,2,3,2,3,4,5,6].uniquelize ());//[1,2,3,4,5,6]Console.log (Array.union (["1","2","3"], ["2","3","4","5","6"], ["5","6","7","8","9"])) Console.log (["2","3","4","5","6"].minus (["1","2","3"]));

JS Array intersection and set of difference set de-weight

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.