JS implementation of array de-weight method Summary (six methods)

Source: Internet
Author: User

Method One:

Two-layer loop, outer loop element, inner loop comparison value

If the same value is skipped, not the same, push into the array

Array.prototype.distinct =function(){ vararr = This, result=[], I, J, Len=arr.length; for(i = 0; i < Len; i++){   for(j = i + 1; j < Len; J + +){   if(Arr[i] = = =Arr[j]) {J= ++i; }} result.push (Arr[i]); } returnresult;}varARRA = [1,2,3,4,4,1,1,2,1,1,1];arra.distinct (); //back to [3,4,2,1]

Method two: Using splice to operate directly in the original array

Two-layer loop, outer loop element, inner loop comparison value

If the value is the same, delete the value

Note: After you delete an element, you need to reduce the length of the array by 1.

Array.prototype.distinct =function (){ vararr = This, I, J, Len=arr.length; for(i = 0; i < Len; i++){   for(j = i + 1; j < Len; J + +){   if(Arr[i] = =Arr[j]) {Arr.splice (J,1); Len--; J--; }  } } returnarr;};varA = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,];varb =a.distinct (); Console.log (b.tostring ());//1,2,3,4,5,6,56

Advantages: Easy to understand

Cons: High memory consumption, slow speed

Method Three: Using the properties of the object can not be the same characteristics to go to the weight

Array.prototype.distinct =function (){ vararr = This, I, obj={}, result=[], Len=arr.length; for(i = 0; i< arr.length; i++){  if(!obj[arr[i]]) {//If you can find it, it proves that the array element repeats.Obj[arr[i]] = 1;  Result.push (Arr[i]); } } returnresult;};varA = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,];varb =a.distinct (); Console.log (b.tostring ());//1,2,3,4,5,6,56

Method Four: Array recursion weight

Using the idea of recursion

Sort first, then compare from the last, encounter the same, then delete

Array.prototype.distinct =function (){ vararr = This, Len=arr.length; Arr.sort (function(A, B) {//sorting arrays for easy comparison  returnAb;}) functionLoop (index) {if(Index >= 1){   if(Arr[index] = = = Arr[index-1]) {arr.splice (index,1); } Loop (Index-1);//recursive loop function for de-weight}} loop (Len-1); returnarr;};varA = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,56,45,56];varb =a.distinct (); Console.log (b.tostring ()); //1,2,3,4,5,6,45,56

Method Five: Using IndexOf and foreach

Array.prototype.distinct =function (){ vararr = This, result=[], Len=arr.length; Arr.foreach (function(V, I, arr) {//The Map,filter method can also be used here to achieve  varBOOL = Arr.indexof (v,i+1);//start looking for duplicates from the next index value passed in the parameter  if(bool = = =-1) {Result.push (v); } }) returnresult;};varA = [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,3,2,3,3,2,2,1,23,1,23,2,3,2,3,2,3];varb =a.distinct (); Console.log (b.tostring ());//1,23,2,3

Method Six: Using the set of ES6

A set data structure, similar to an array, whose member values are unique.

Convert set structure to an array using Array.from

functionreturn array.from (new  Set (Array));} Dedupe ([[+]

Expand Operator (...) Internal use of For...of loops

Let arr = [1,2,3,3= [...] New  //[[+]

The following is an introduction to the method of merging arrays and going to heavy

One, concat () method

Idea: the Concat () method merges an incoming array or non-array value with the original array, forming a new array and returning it. The method produces a new array.

function Concatarr (arr1, arr2) {  var arr = arr1.concat (arr2);   = Unique1 (arr); // and then refer to any of the above methods  of de-weight return arr;}

Second, Array.prototype.push.apply ()

Idea: The advantage of this method is that it does not produce a new array.

var a = [1, 2, 3]; var b = [4, 5, 6]; Array.prototype.push.apply (A, b); // a=[1,2,3,4,5,6] // equivalent to: A.push.apply (A, b); //  function  concatarray (arr1,arr2) {  Array.prototype.push.apply (arr1, arr2);   = unique1 (arr1);   return arr1;}

  

JS implementation of array de-weight method Summary (six methods)

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.