<script>
//法一:
let arr=['a','b','c','a','c','a','d','c'];
function one(){
let br=[];
for(var i in arr){
if(br.indexOf(arr[i])<0){
br.push(arr[i]);
}
}
console.log(br);
}
one();
//法二:
Array.prototype.uA=function(){
let br=[];
this.forEach(function(item){
if(br.indexOf(item)==-1){
br.push(item);
}
});
return br;
}
let b=[1,1,1,2,3,4,1,1,2,3].uA();
console.log(b);
//法三:
let arr=[1,2,3,5,4,3,21,11,1,2,2,1,1,4];
let uA=arr=>Array.from(new Set(arr));
let newArr=uA(arr);
console.log(newArr);
//法四: (注:在原数组上去重)
let arr=[100,200,2,100,3,3,200];
Array.prototype.uA=function(){
for(var i=0;i<this.length;i++){
if(this.indexOf(this[i])!=i){
this.splice(i,1);
i--;
}
}
}
arr.uA();
console.log(arr);
//法五:
//... 扩展运算符 rest参数的逆运算
let arr=[100,200,2,100,3,3,200];
let uA=arr=>[...new Set(arr)];
let uA=function(arr){
return [...new Set(arr)];
}
console.log(uA(arr));
//法六
var set =new Set([100,200,2,100,3,3,200]);
var s=Array.from(set);
console.log(s);
</script>