JS array are two ways to go heavy

1 deduplication array

let arr = [1,2,3,2,5,3,1,NaN, null, false, undefined, false, NaN, null, undefined];

  ES5 achieve

// foreach loop
deleteCopy(arr){      
  let obj={};
  let newArr = [];
  // key value using the object  
  arr.forEach((item,index) => { 
    if(!obj[item]){
      obj[item] = true;
      newArr.push(item);
    }             
  });
  console.log(newArr); 
  // [1, 2, 3, 5, NaN, null, false, undefined]
},

  ES6  

// set the return of an object, with the Array.form, the class could be an array of objects, objects can be converted into an array of iterations
deleteCopyES6(arr){
  console.log(Array.from(new Set(arr)))
  // [1, 2, 3, 5, NaN, null, false, undefined]
},

  

Guess you like

Origin www.cnblogs.com/webfont-yxw/p/11735764.html