Merging of arrays, de-weight, sorting

Source: Internet
Author: User

One, the combined de-weight of two arrays:

vararr1 = [' A ', ' B '];varARR2 = [' A ', ' C ', ' d '];varARR3 = [1, ' d ', undefined,true,NULL];//merge two arrays, go to heavyvarConcat_ =function(ARR1,ARR2) {//do not directly use the var arr = arr1, so that arr is only a reference to ARR1, and the modifications of both will affect each other    vararr =Arr1.concat (); //or use slice () to copy, var arr = arr1.slice (0)     for(vari=0;i<arr2.length;i++) {arr.indexof (arr2[i])= = =-1? Arr.push (Arr2[i]): 0; }    returnarr;}   Console.log (Concat_ (ARR1,ARR2)); //[' A ', ' B ', ' C ', ' d ']

Two, the merging of multiple arrays to remove weight:

vararr1 = [' A ', ' B '];varARR2 = [' A ', ' C ', ' d '];varARR3 = [1, ' d ', undefined,true,NULL];//merge multiple arrays, go to heavyvarConcat_ =function(ARR1,ARR2,ARR3) {if(Arguments.length <= 1){        return false; }    varConcat_ =function(ARR1,ARR2) {vararr =Arr1.concat ();  for(vari=0;i<arr2.length;i++) {arr.indexof (arr2[i])= = =-1? Arr.push (Arr2[i]): 0; }        returnarr; }    varresult =Concat_ (ARR1,ARR2);  for(vari=2;i<arguments.length;i++) {result=Concat_ (Result,arguments[i]); }    returnresult;} Console.log (Concat_ (ARR1,ARR2,ARR3));//["A", "B", "C", "D", 1, undefined, true, NULL]

Three, multiple arrays merged, de-heavy, sort:

//merge multiple arrays, go to heavy, sortvarARR1 = [1,6,4,0];varARR2 = [8,20,7,4.5];varARR3 = [6,0,7,90,2];varConcat_ =function(ARR1,ARR2,ARR3) {if(Arguments.length <= 1){        return false; }    varConcat_ =function(ARR1,ARR2) {vararr =Arr1.concat ();  for(vari=0;i<arr2.length;i++) {arr.indexof (arr2[i])= = =-1? Arr.push (Arr2[i]): 0; }        returnarr; }    varresult =Concat_ (ARR1,ARR2);  for(vari=2;i<arguments.length;i++) {result=Concat_ (Result,arguments[i]); }    //Sort    functionSortnumber (A, b) {returnAb; }    returnResult.sort (sortnumber);} Console.log (Concat_ (ARR1,ARR2,ARR3))//[0, 1, 2, 4, 4.5, 6, 7, 8, +--]

Reference:

http://m.blog.csdn.net/article/details?id=49634041

Merging of arrays, de-weight, sorting

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.