方法一:遍历数组,利用indexOf方法判断是否重复
var arr= ['a', 'a', 'b', 'b', 'c', 'c'];
var array =[];
for(var i =0; i < arr.length; i++) {
if(array.indexOf(arr[i]) ===-1) {array.push(arr[i]) }
}
console.log(array);//['a', 'b', 'c']
方法二:遍历数组,利用include方法判断是否重复
var arr= ['a', 'a', 'b', 'b', 'c', 'c'];
var array =[];
for(var i =0; i < arr.length; i++) {
if(!array.includes(arr[i]) ) {array.push(arr[i]) }
}
console.log(array);//['a', 'b', 'c']
方法三:利用Array.from(new Set())去重,此法只能用于兼容ES6的浏览器
var arr= ['a', 'a', 'b', 'b', 'c', 'c'];
var array =[];
array = Array.from(new Set(arr));
console.log(array);//['a', 'b', 'c']