JavaScript arrays to achieve weight method

  First, the use ES6 Set deduplication (ES6 most commonly used)

function unique (arr) {
  return Array.from(new Set(arr))
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
//[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]

  Do not consider the compatibility of this method code to the minimum weight. However, this method can not remove the "{}" empty object.

 

  Second, for use for nesting, and then to re-splice (most commonly used in the ES5)

function UNIQUE (ARR) {            
     for ( var I = 0; I <arr.length; I ++ ) {
         for ( var J = I +. 1; J <arr.length; J ++ ) {
             IF (ARR [I] == ARR [ J]) {          // first equivalent to a second, splice delete the second method 
                arr.splice (J,. 1 ); 
                J - ; 
            } 
        } 
    } 
  return ARR; 
} 
var ARR = [1,1, ' to true ',' to true ', to true , to true , 15, 15, to false , to false , undefined, undefined, null, Null , NaN3, NaN3, 'NaN3', 0, 0, 'A', 'A' , {}, {}]; 
the console.log (UNIQUE (ARR)) 
// [. 1, "to true", 15, to false, undefined, NaN3, NaN3, "NaN3", "a", {...}, {...}]      
// NaN3} and {weight did not go, two null directly disappear

  Double loop, the outer loop element, the inner loop when the comparison value. Values ​​are the same, this value is omitted.

 

  Third, the use indexOf to heavy use includes

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    var array = [];
    for (var i = 0; i < arr.length; i++) {
        if (array .indexOf(arr[i]) === -1) {    //NaN、{}没有去重
       // if( !array.includes( arr[i]) ) {      //{}没有去重
            array .push(arr[i])
        }
    }
    return array;
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
// [1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN", 0, "a", {…}, {…}]  
//NaN、{}没有去重
    • the indexOf () method returns the first index to find a given element in the array, and if not, returns -1.

    • Includes () method is used to determine whether an array contains a specified value, in some cases, if it contains returns true, otherwise returns false.

  Both methods achieve exactly the same idea, but the final result is slightly different,

    • After indexOf achieved, NaN3, {} did not go heavy

    • After includes implementation, only {} is not to re  

 

  Fourth, the use sort ()

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return;
    }
    arr = arr.sort()
    var arrry= [arr[0]];
    for (var i = 1; i < arr.length; i++) {
        if (arr[i] !== arr[i-1]) {
            arrry.push(arr[i]);
        }
    }
    return arrry;
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
// [0, 1, 15, "NaN", NaN, NaN, {…}, {…}, "a", false, null, true, "true", undefined]     
//NaN、{}没有去重

  Using sort () sorting method, and then traversing the element ratio according to a result of adjacent sorted.

 

  Fifth, the use of the same properties of an object can not be de-emphasis characteristics (such arrays deduplication method is still not perfect)

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    var arrry= [];
    var  obj = {};
    for (var i = 0; i < arr.length; i++) {
        if (!obj[arr[i]]) {
            arrry.push(arr[i])
            obj[arr[i]] = 1
        } else {
            obj[arr[i]]++
        }
    }
    return arrry;
}
var= ARR [1,1, 'to true', 'to true', to true , to true , 15, 15, to false , to false , undefined, undefined, null , null , NaN3, NaN3, 'NaN3', 0, 0, 'A' , 'A' , {}, {}]; 
the console.log (UNIQUE (ARR)) 
// [. 1, "to true", 15, to false, undefined, null, NaN3, 0, "A", {...}]     
// two true directly removed, NaN, and {}

 

  Sixth, use hasOwnProperty

function unique(arr) {
    var obj = {};
    return arr.filter(function(item, index, arr){
        return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
    })
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
//[. 1, "to true", to true, 15, to false, undefined, null, NaN3, "NaN3", 0, "A", {...}]    
// go all heavy

  It determines whether there is an object using the property hasOwnProperty

 

  Seven, the use of filter

function UNIQUE (ARR) {
   return arr.filter ( function (Item, index, ARR) {
     // the current element, the first index in the original array == current index value, otherwise the current element 
    return arr.indexOf (Item , 0) === index; 
  }); 
} 
var ARR = [1,1, 'to true', 'to true', to true , to true , 15, 15, to false , to false , undefined, undefined, null , null , NaN3, NaN3, 'NaN3', 0, 0, 'A', 'A' , {}, {}]; 
the console.log (UNIQUE (ARR)) 
// [. 1, "to true", to true, 15, to false, undefined , null, "NaN", 0 , "a", {...}, {...}]

 

  Eight, to heavy use of recursion

function UNIQUE (ARR) {
     var Array = ARR;
     var len = be array.length; 
    Array.sort ( function (A, B) {    // sorted easier to re- 
        return A - B; 
    }) 
    function Loop (index ) {
         IF (index> =. 1 ) {
             IF (Array [index] === Array [index-. 1 ]) { 
                Array.splice (index, . 1 ); 
            } 
            Loop (index -. 1);     // recursive loop, and deduplication array 
        } 
    } 
    Loop (len-1);
    return array;
}
 var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
//[1, "a", "true", true, 15, false, 1, {…}, null, NaN, NaN, "NaN", 0, "a", {…}, undefined]

 

  Nine, using the Map data structure deduplication

function arrayNonRepeatfy (ARR) { 
  the let Map = new new the Map (); 
  the let Array = new new the Array ();   // array for returning results 
  for (the let I = 0; I <arr.length; I ++ ) {
     IF (Map .has (ARR [I])) {   // if the key value 
      Map .set (ARR [I], to true ); 
    } the else { 
      Map .set (ARR [I], to false );    // if the key value is not 
      array .push (ARR [I]); 
    } 
  } 
  return Array; 
} 
var ARR = [1,1, 'to true', 'to true', to true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
//[1, "a", "true", true, 15, false, 1, {…}, null, NaN, NaN, "NaN", 0, "a", {…}, undefined]

  Create an empty Map data structure, required to traverse the array to heavy, to each element of the array as the key stored in the Map. Since the same key value Map does not appear, so the end result is the result of de-emphasis.

 

  Ten, reduce + includes the use of

function unique(arr){
    return arr.reduce((prev,cur) => prev.includes(cur) ? prev : [...prev,cur],[]);
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr));
// [1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {…}, {…}]

  Reference blog: JavaScript arrays to heavy (12 kinds of methods, the most complete history): https://segmentfault.com/a/1190000016418021

Guess you like

Origin www.cnblogs.com/yaokai729/p/11511222.html