Situation:

Var arr = array (1, 1, ‘true’, ‘true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, “NaN”, 0, 0,’ a ‘, ‘a’, {}, {}]; Filter out duplicate values in

1, the ES6 – set

Using SET in ES6 is the easiest way to de-duplicate

var arr  = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];function arr_unique1(arr){
return  [...new Set(arr)];
/ / or
//return Array.from(new Set(arr));
}
arr_unique1(arr); / / (13) [1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
Copy the code

This approach is arguably the perfect one, requiring an environment that supports ES6

2. Use Map data structure for deduplication

Create an empty Map data structure that iterates through the array to be repealed, storing each element of the array as a key in the Map. Since the Map does not have the same key value, the final result is the result after the deduplication.

function arr_unique2(arr) {
  let map = new Map(a);let array = new Array(a);// The array is used to return the result
  for (let i = 0; i < arr.length; i++) {
    if(map .has(arr[i])) {  // If there is a key value
      map .set(arr[i], true);
    } else {
      map .set(arr[i], false);   // If there is no key valuearray .push(arr[i]); }}return array ;
}

 console.log(arr_unique2(arr)); / / (13) [1, "a", "true", true, 15, false, 1, {...}, null, NaN, NaN, "NaN," 0, "a", {...}, undefined]
Copy the code

3. Use recursion to duplicate

function arr_unique3(arr) {
     var array= arr;
     var len = array.length;
     array.sort(function(a,b){   // It is more convenient to remove the weight after sorting
     return a - b;
    })
    
 function loop(index){
        if(index >= 1) {if(array[index] === array[index- 1]){
                array.splice(index,1);
            }
            loop(index - 1);    // loop recursively, then the array is deduplicated
        }
    }
    loop(len- 1);
    return array;
}
 
console.log(arr_unique3(arr)); / / (14) [1, "a", "true", true, 15, false, 1, {...}, null, NaN, NaN, "NaN," 0, "a", {...}, undefined]
Copy the code

4、 forEach + indexOf

function arr_unique4(arr){
var res = [];
arr.forEach((val,index) = >{
if( res.indexOf(val) === - 1){ res.push(val); }});return res;
}

console.log(arr_unique4(arr)); / / (14) [1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN," 0, "a", {...}, {...}]
Copy the code

The disadvantage of this method is that NaN cannot be filtered because var a = [1, NaN, 2]; a.indexOf(NaN) === -1; The improvement method is to use an includes method

5, the filter + indexOf

function arr_unique5(arr){
return  arr.filter((val,index,item) = >{
return item.indexOf(val) === index;
});
}

arr_unique5(arr); / / (12) (1, "true", true, 15, false, undefined, null, "NaN," 0, "a", {...}, {...}]
Copy the code

The fly in the ointment is the omission of NaN, for the same reason as Method four

6, includes

function arr_unique6(arr){
var res = [];
arr.forEach((val) = >{
if(! res.includes(val) ){ res.push(val); }});return res;
}
arr_unique6(arr); / / (13) [1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
Copy the code

This method is also relatively perfect, nothing missing, right

7、 reduce + includes

function  arr_unique7(arr){
return arr.reduce( (prev, cur ) = >{
if(! prev.includes(cur) ){ prev.push(cur); }returnprev; } []); } arr_unique7(arr);/ / (13) [1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
Copy the code

Nested loops +splice

function arr_unique8(arr){
for(var i = 0 ; i < arr.length; i++){
for( var j = i + 1; j < arr.length; j++){
if( arr[i] === arr[j] ){
arr.splice(j,1); }}}return arr;
}
arr_unique8(arr); / / (14) [1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN," 0, "a", {...}, {...}]
Copy the code

This is the most cumbersome method and is very inefficient, as each loop dynamically fetches the length of the array. And this method cannot filter out NaN because NaN === NaN results in false.

9, Hash +hasOwnProperty+ json.stringify (final version)

function arr_unique9(arr){
var hash = {};
return arr.filter( (val) = >{
return hash.hasOwnProperty( typeof  val + JSON.stringify(val) ) ? false : hash[typeof val + JSON.stringify(val)] = true ;
});
}
arr_unique9(arr); / / (12) (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}]
Copy the code

This method is the ultimate in unduplicating object elements in arrays! The previous methods do not allow object de-duplication. Then again, an object in JS is itself a reference to an address, such as {} == {}; //false, the two are two different objects, which I simplify with json.stringify.

According to the personal test, the Set and Map efficiency of ES6 is the highest, reduce() and sort() efficiency is ok, and the double-layer cycle efficiency is the lowest.

Array to major full, you, get it ~

The latter

Small life is the front small white one, the most original intention of writing an article is to make study notes, in order to let oneself have a more profound impression and understanding of the knowledge point, write things is also very small white, if there is wrong, welcome to correct ~ then is the hope to see the children’s shoes can point like, can also pay attention to a wave ~ I will continue to output!

My blog site link

CSDN homepage

Nuggets personal homepage

Jane book personal home page