JS0 -- 去重

1.双重for

function dr( arr ){            //Duplicate removal
  let aNews = arr;
  for(let i = 0; i < arr.length; i++){
    for(let j = i+1; j < arr.length; j++){
      if( arr[i] == arr[j] && i!=j){  
        arr.splice(j,1)
j--       }     }   }   return aNews; }

2.set数组对象,array.from数组对象转数组

function dr (arr) {

  return Array.from(new Set(arr))    //    方法2: return [new Set(arr)]

}

3. indexof

function dr(arr) {
  let newArr = [];
  for(let item of arr) {
    if(newArr.indexOf(item) == -1) {
      newArr.push(item)  
    }
  }
  return newArr;    
}

链接:https://segmentfault.com/a/1190000016418021?utm_source=tag-newest

原文地址:https://www.cnblogs.com/lgyong/p/8490610.html