简介:堪称史上最全数组去重
//一、set(es6新增) /* function fn(arr){ return Array.from(new Set(arr)); } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //二、splice /* function fn(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); j--; } } } return arr; } arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)) */ //三、indexOf /* function fn(){ 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)){ array.push(arr[i]);//把array数组中没有的数据放进array中 } } } */ //四、sort() /* function fn(arr){ if(!Array.isArray(arr)){ console.log('type error!') return; } arr=arr.sort();//先排序 var array=[arr[0]]; for(var i=1;i<arr.length;i++){ if(arr[i]!==arr[i-1]){ array.push(arr[i]);//把排序好的数组里的元素与后一个元素进行对比,不一样的放入新数组 } } return array; } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //五、对象属性不能相同 /* function fn(arr){ if(!Array.isArray(arr)){ console.log('type error!'); return; } var array=[]; var obj={}; for(var i=0;i<arr.length;i++){ if(!obj[arr[i]]){ array.push(arr[i]); obj[arr[i]]=1; }else{ obj[arr[i]]++; } } return array; } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //六、includes /* function fn(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])){ array.push(arr[i]); } } return array; } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //七、hasOwnProperty /* function fn(arr){ var obj={}; return arr.filter(function(item,index,arr){ return obj.hasOwnProperty(typeof item+item)?false:(obj[typeof item+item]=true) }) } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //八、filter /* function fn(arr){ return arr.filter(function(item,index,arr){ return arr.indexOf(item,0)===index; }); } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //九、不能给字符串去重 /* function fn(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(len-1); return array; } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //十、map /* function fn(){ let map=new Map(); let array=new Array(); for(let i=0;i<arr.length;i++){ if(map.has(arr[i])){ map.set(arr[i],true); }else{ map.set(arr[i],false); array .push(arr[i]); } } return array; } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //十一、reduce+includes /* function fn(arr){ return arr.reduce((prev,cur)=>prev.includes(cur)?prev:[...prev,cur],[]) } var arr=[11,11,22,22,22,33,33,44,44,44,55,55,"a","b","c","a","b","c"]; console.log(fn(arr)); */ //十二、...new set(arr) let arr1 = [1, 2, 6, 54, 3, 6, 8, 9, 23, 55]; let arr2 = [...new Set(arr1)] console.log(arr2);