The number of JavaScript combinations and the weight

Source: Internet
Author: User

One, two methods of array merging

1, concat--Merge array, and do not go heavy

var arr1 = ["AB", "BC", "de", "FG"]; var arr2 = ["ml", "kg", "ww", "BC", "AM"];var arr3 = Arr1.concat (ARR2);
Console.log (ARR3)

2. Custom array merging and de-resetting functions

varARR1 = ["AB", "BC", "de", "FG"];varARR2 = ["ml", "kg", "ww", "BC", "AM"];functionMergearray (arr1,arr2) {var_arr =NewArray ();  for(vari=0;i<arr1.length;i++) {_arr.push (arr1[i]); }     for(vari=0;i<arr2.length;i++){        varFlag =true;  for(varj=0;j<arr1.length;j++){            if(arr2[i]==Arr1[j]) {Flag=false;  Break; }        }        if(flag) {_arr.push (arr2[i]); }    }    return_arr;} Console.log (Mergearray (ARR1,ARR2));

Second, single array de-weight method

1. Traversal Array method

This is the simplest way to do it: to iterate over the old array and add the value to the newly created array (when the value does not exist in the new array)

//the simplest array de-weight methodfunctionUniquearr (array) {varn = [];//a new temporary array    //iterate through the current array     for(vari = 0; i < Array.Length; i++) {        //if the current array of I has been saved into a temporary array, then skip,        //Otherwise push the current item to the temporary array        if(N.indexof (array[i]) = =-1) N.push (Array[i]); }    returnN;}

Note: IndexOf is a new method for ECMA5, IE8 and below are not supported, so compatibility processing is done.

if(Array.prototype.indexOf) {Array.prototype.indexOf=function(item) {varresult = -1,a_item =NULL; if( This. length = = 0) {      returnresult} for(vari = 0,len= This. length;i<len;i++) {A_item= This[i]; if(A_item = = =Item) {Result=i;  Break; }    }    returnresult; }}

2. Object key value pair method

The method executes faster than any other method, but it takes up a bit more memory

Implementation of the idea: Create a new JS object and the newly array, traversing the array to go to the heavy, determine whether the value is the key of the JS object. If not, add the key to the object and put it in the new array.

Note: When you determine whether an object key is being called, "toString ()" is automatically executed for the key passed in, and the different keys may be mistaken for the same, such as: a[1] and a[' 1 ']. To resolve this problem, you will have to call "IndexOf".

functionUniquearr (arr) {varn ={}, R=[], Len=Arr.length, Val, type;  for(vari = 0; i < Len; i++) {Val=Arr[i]; Type=typeofVal; if(!N[val]) {N[val]=[Type];            R.push (Val); } Else if(N[val].indexof (type) < 0) {N[val].push (type);            R.push (Val);        }        }; returnR; } console.log (Uniquearr ([1,2,[1,2,3],, ' 1 ', 2, ' 4 ', ' 6 '])

3. Array subscript Judgment method

Still have to call "IndexOf" performance is similar to Method 1,
Implementation idea: If the current array of item I is in the current array the first occurrence of the position is not I, then the term I is repeated, ignored. Otherwise, the result array is deposited.

Disadvantage: If the value in the array is undefined, it will be rejected, not in the new array.

 functionUniquearr (array) {varn = [];//result Array        //iterate from the first entry         for(vari = 0; i < Array.Length; i++) {            //if the item I of the current array first appears in the current array, the position is not I,            //then it means that item I is repeated and ignored. Otherwise, the result array is stored            if(Array.indexof (array[i]) = =i) N.push (Array[i]); }        returnN; } console.log (Uniquearr ([1, 2, [1, 2, 3],, ' 1 ', 2, ' 4 ', ' 6 '])

4, after sorting the adjacent division

Although the "sort" method of the native array is not very reliable, it has no effect on the weight of the order.
Implementation ideas: Sort incoming arrays, sort the same values next to each other, and then iterate through the array to add only values that do not duplicate the previous value.

function Uniquearr (array) {        array.sort ();         var n = [];          for (var i = 0; i < array.length; i++) {            if (Array[i]!== n[n.length-1]) {                n. Push (Array[i]);            }        }         return n;    }    Console.log (Uniquearr ([1, 2, [1, 2, 3],, ' 1 ', 2, ' 4 ', ' 6 ']))

5. Optimized traversal Array method

Originating from a foreign blog, the implementation code of the method is quite cool;
Implementation idea: Gets the right-most value that is not duplicated into the new array. (The next round of judgment that terminates the current loop and enters the top loop when a duplicate value is detected)

// idea: Get the right-most value without duplicates into a new array function Uniquearr (array) {    var r = [];      for (var i = 0, L = array.length; i < L; i++) {        for (var j = i + 1; J < l ; J + +)            if (array[i] = = = Array[j]) j = + +i;        R.push (Array[i]);    }     return R;} Console.log (Uniquearr ([1, 2, [1, 2, 3],, ' 1 ', 2, ' 4 ', ' 6 ']))

The number of JavaScript combinations and the weight

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.