9, the front end of knowledge - to re-summary

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

1  // deduplication string 
2 the let STR = "12qwe345671dsfa233dsf9876ds243dsaljhkjfzxcxzvdsf" ;
 . 3 the let Arrays str.split = ( "")   // To first converted into a string array 
. 4  the console.log (Arrays)
 . 5 the console.log (the Array. from ( new new the Set (Arrays)))
 . 6 the console.log (Array.from ( new new the Set (Arrays)). the Join ( '' ))
 . 7  
. 8  function uniqueStr (STR) {
 . 9      the let str.split ARR = ( '' )
 10      the let arrNew = Array.from ( new new the Set (ARR))
 . 11      return arrNew.join ( '' )
 12 is }
13 console.log(uniqueStr(str))
14 //12qwe34567dsfa98ljhkzxcv
15 
16 //去重数组
17 var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
18 function uniqueArr(arr){
19     let arrNew = Array.from(new Set(arr))
20     return arrNew
21 }
22 console.log(uniqueArr(arr))
23 // 返回: [1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]

Do not consider the compatibility of this method code to the minimum weight. This method can not remove the "{}" empty object, the latter method adds high order "{}" methods to remove the duplicate.

 

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

 1 var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
 2 function unique(arr){
 3     for (var i = 0; i < arr.length; i++){
 4         for( var j = i+1; j < arr.length; j++){
 5             if( arr[i] === arr[j]){
 6                 arr.splice(j,1)
 7             }
 8         }
 . 9      }
 10      return ARR
 . 11  }
 12 is  the console.log (UNIQUE (ARR))
 13 is  // Return value: [1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN", 0 , "a", {}, {}], NaN two and two did not remove {}

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 deduplication

 1 var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
 2 function unique(arr){
 3     if(!Array.isArray(arr)){
 4         console.log('type error')
 5         return
 6     }
 7     let array = []
 8     for( var i = 0; i < arr.length; i++){
 9        if(The Array.indexOf (ARR [I]) === -1 ) {// This -1 for? ?
10              Array.push (ARR [I])  
 . 11         }
 12 is      }
 13 is      return Array;
 14  }
 15  the console.log (UNIQUE (ARR))
 16  // Return value: [1, "true", true, 15, false, undefined , null, NaN, NaN, " NaN", 0, "a", {}, {}], NaN two and two did not remove {}

Create an empty result array, for an array of the original loop, the determination result whether there is an array of the current element, if the same value is skipped, the same does not push into the array.

Fourth, the use sort

 1 var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
 2 function unique(arr){
 3     if(!Array.isArray(arr)){
 4         console.log('type error')
 5         return
 6     }
 7     arr = arr.sort()
 8     var array = [arr[0]]
 9 
10     for( var i = 1; i < arr.length; i++){
11         if( arr[i] !== arr[i-1]){
12             array.push(arr[i])
13         }
14     }
15     return array
16 }
17 console.log(unique(arr))
18 //返回值:(14) [0, 1, 15, NaN, NaN, "NaN", {…}, {…}, "a", false, null, "true", true, undefined]

Using a 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 a weight to an array method in question, is not recommended, be improved)

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,'true','true',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, "true", 15, to false, undefined, null, NaN, 0, "a", {...}] // true two directly removed, NaN} and {deduplication

 

Sixth, 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.includes( arr[i]) ) {//includes 检测数组是否有某个值
          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", 0, "a", {…}, {…}]   //{}没有去重

Seven, the use of 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, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {…}]  //所有的都去重了

  Use hasOwnProperty is determined whether there is an object property

Eight, the use of filter

UNIQUE function (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", {...}, {...}]

 

Nine, to heavy use of recursion

function unique(arr) {
    var array= arr;
    var len = array.length;
  array.sort(function(a,b){  //排序后更加方便去重
    return a - b;
  })
  function loop(index){
    if(index >= 1){
      if(array[index] === array[index-1]){
        array.splice(index,1);
      }
      loop(index - 1);  //递归loop,然后数组去重
    }
  }
  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]

X. Map data structure using deduplication

function arrayNonRepeatfy(arr) {
 let map = new Map();
 let array = new Array(); // 数组用于返回结果
 for (let i = 0; i < arr.length; i++) {
  if(map .has(arr[i])) { // 如果有该key值
   map .set(arr[i], true);
  } else {
   map .set(arr[i], false);  // 如果没有该key值
   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, "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.

 

XI using reduce + includes

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", {…}, {…}]

  

Guess you like

Origin www.cnblogs.com/jianguo221/p/11776325.html