The method of de-weight for arrays

Source: Internet
Author: User

Sometimes there are repeating elements in the array, and we don't need to do that for the arrays.

There are many ways to go heavy, and here are several ways to easily understand them.

1, first to the simplest method

Use the new data structure set provided by ES6. It is similar to an array, but its element values are unique and are not duplicated.

1 Let arr = [1,1,1,1,1,1,2,3,4,1,2,3,4]2 arr = Array.from (new  Set (arr)) 3 Console.log (arr) 4 // [1, 2, 3, 4]
2. Use the IndexOf () method of the string. This method searches for the given substring from a string and returns the position of the substring (1 if not found). Arrays can be used as well.
Let arr = [1,1,1,1,1,1,2,3,4]function  Distinct (arr) {  = [],len=arr.length;   = =    {var bool = Arr.indexof (V, i+1)    ; if (bool = = = 1) {      Result.push (v);    }  })  ; return result;} Console.log (DISTINCT (arr))//  [1, 2, 3, 4]
3, using the Splice () method of the array. Double loops determine if they are equal, and then delete one of the following. But it changes the original array.
1Let arr = [1,1,1,1,1,1,2,3,4]2 3 functionDistinct (arr) {4Let I, J, len=arr.length;5    for(i=0;i<len;i++){6      for(j=i+1;j<len;j++){7       if(Arr[i] = =Arr[j]) {8Arr.splice (j,1);9len--;Tenj--; One       } A     } -   } -   returnarr; the } - Console.log (DISTINCT (arr)) - //[1, 2, 3, 4]
4, the use of the Filter method and object properties exist to come and go heavy.
Let arr = [1,1,1,1, ' 1 ', 1,2,3,4, ' 4 ']function  Unique (arr) {  = {};   return arr.filter (item) = {    return obj.hasownproperty (item)?  False:(obj[item]=true)  })} Console.log (arr)//  [1, 2, 3, 4]

As seen above, 1 and ' 1 ' cannot be distinguished, so this method only applies to arrays of pure numbers.

5, the use of the object's attribute value exists
Let arr = [1,1,1,1,1,1,2,3,4, ' 4 ', ' 4 ', ' 4 ']function  -Unique (arr) {let  obj={},  Item,key,  = [];    for (Let i=0,len=arr.length;i<len;i++) {    = arr[i];     typeof (item) + item;     if (! Obj[key]) {      Res.push (item);       = item;    }  }   return Res;} Console.log (arr)//  [1, 2, 3, 4, ' 4 ']

This method can differentiate between numbers and characters, which is better than the previous method.

6, the general method. Double loops determine whether elements are equal.
Let arr = [1,1,1,1,1,1,2,3,4, ' 4 ', ' 4 ', ' 4 ']functionDistinct (arr) {varresult = [];  for(Let i=0,len=arr.length;i<len;i++){     for(Let j=i+1;j<len;j++){      if(Arr[i] = = =Arr[j]) {J= ++I//Console.log (j)}} result.push (Arr[i])}returnresult;} Console.log (DISTINCT (arr))//[1, 2, 3, 4, ' 4 ']

These are several easy-to-understand methods for commonly used array de-weight.

The method of de-weight for arrays

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.