Sorting out a wave of array de-weight methods

Source: Internet
Author: User
Tags object object

About the array to heavy, has been using the loop, there are es6 set, access to the data there are so many ways to redo, collated the following. Direct on Dry Goods

Loop 1
functionUnique (arr) {varNEWARR = []; varisrepeat;  for(vari=0; i<arr.length; i++) {isrepeat =false;  for(varj=i+1; j<arr.length; J + +) {if(Arr[i] = = =Arr[j]) {Isrepeat=true;  Break; }        }        if(!isrepeat)        {Newarr.push (arr[i]); }    }    returnNEWARR;}vararr = [5,6,1,8,1,6];console.log (arr); 
Loop 2
var newArr = [];  for (var i=0; i<arr.length; i++) {    for (var j=i+1; j<arr.length; j + +) {         if (Arr[i] = = = Arr[j]            ) {= + +i;        }    }    Newarr.push (Arr[i]);} return NEWARR;} var arr = [5,6,1,8,1,6
foreach method

Iterates through the array elements passed in, and if this element is not in the new array, push in the new array

function Unique (arr) {    var newArr = [];    Arr.foreach (function(item) {         if (newarr.indexof (item) = = = 1) {            Newarr.push (item);        }    });    return NEWARR;} var arr = [5,6,1,8,1,6      
The Filter method item represents each element in the array, and index is where each element appears. IndexOf back to the horse. first oneIndex.
function Unique (arr) {    return Arr.filter (function (item, index) {        return Arr.indexof (item) = = = Index;    });}
Sort method

First sort, and then compare whether the adjacent is the same, the different push into the new array

function Unique (arr) {    var newArr = [];                      Arr.sort ();      for (var i = 0; i < arr.length; i++) {        if(Arr[i]!== arr[i+1]) {            Newarr.push ( Arr[i]);        }    }     return NEWARR;} var arr = [5,6,1,8,1,6
Sort Method 2

Sort the array, push the first element into the new array, and compare each element to be put into the last element of the new array, and push

function Unique (arr) {    var newArr = [];                      Arr.sort ();     var newArr = [arr[0]];      for (var i = 1; i < arr.length; i++) {        if(Arr[i]!== newarr[newarr.length-1]) {        Newarr.push (Arr[i]);        }    }         return NEWARR;} var arr = [5,6,1,8,1,6
Object

The array value is used as the property of the object and is not assigned if repeated.

function Unique (arr) {    var newArr = [];     var tmp = {};      for (var i=0; i<arr.length; i++) {        if(!  Tmp[arr[i]]) {            = 1;            Newarr.push (Arr[i]);        }    }     return NEWARR;} var arr = [5,6,1,8,1,6

But be aware that this method cannot handle all situations

1. Cannot distinguish between implicitly typed values that are converted to strings, such as 1 and ' 1 '.

2. You cannot handle complex data types, such as objects (because the object becomes an ["Object Object]" as a key).

3. Special data, such as ' __proto__ ', because the __proto__ property of the TMP object cannot be overridden.

Object Upgrade 1
functionUnique (arr) {varNEWARR = []; varTMP = {}; varTmpkey;  for(vari=0; i<arr.length; i++) {Tmpkey=typeofArr[i] +Arr[i];         Console.log (Tmpkey); if(!Tmp[tmpkey]) {Tmp[tmpkey]= 1;        Newarr.push (Arr[i]); }    }    returnNEWARR;}vararr = [5, 6, ' 1 ', 8,1,6];console.log (arr); 

Object Upgrade 2
functionUnique (arr) {varNEWARR = []; varTMP = {}; varTmpkey;  for(vari=0; i<arr.length; i++) {Tmpkey=typeofArr[i] +json.stringify (Arr[i]); Console.log (Tmpkey)if(!Tmp[tmpkey]) {Tmp[tmpkey]= 1;        Newarr.push (Arr[i]); }    }    returnNEWARR;}vararr = [5,6,1,8,1,6];console.log (arr); 
ES6 Map

Map is a new type of data and a collection of key-value pairs, but the range of keys is not limited to strings, and various types of values (including objects) can be treated as keys.

function Unique (arr) {    var newArr = [];     var New Map ();      for (var i=0; i<arr.length; i++) {        if(!  Tmp.get (Arr[i])) {            1);            Newarr.push (Arr[i]);        }    }     return NEWARR;} var arr = [5,6,1,8,1,6
ES6 Set

Array.from()method to create a new array instance from a similar array or an iterative object.

function Unique (arr) {    varnew  Set (arr);     return Array.from (set);} var arr = [5,6,1,8,1,6
Includes () method

The includes () method is used to determine whether an array contains a specified value, and if so, returns True if it is included, otherwise false.

function Unique (arr) {    var newArr = [];    Arr.foreach (function(item) {        if(!  Newarr.includes (item)) {            Newarr.push (item);        }    });     return NEWARR;} var arr = [5,6,1,8,1,6

Sorting out a wave of array de-weight 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.