1. ES6 Set结构
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22];
console.log(arr);
function noRepeat(arr){
var newArr = [...new Set(arr)]; //利用了Set结构不能接收重复数据的特点
return newArr
}
var arr2 = noRepeat(arr)
console.log(arr2);
2. filter()
var arr = ['apple','apps','pear','apple','orange','apps'];
console.log(arr)
var newArr = arr.filter(function(item,index){
return arr.indexOf(item) === index; // 因为indexOf 只能查找到第一个
});
console.log(newArr);
3. for循环搭配indexOf
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22];
function noRepeat(arr) {
//定义一个新的临时数组
var newArr=[];
//遍历当前数组
for(var i=0;i<arr.length;i++) {
//如果当前数组的第i已经保存进了临时数组,那么跳过,
//否则把当前项push到临时数组里面
if(newArr.indexOf(arr[i]) === -1) { //indexOf() 判断数组中有没有字符串值,如果没有则返回 -1
newArr.push(arr[i]);
}
}
return newArr
}
var arr2 = noRepeat(arr);
console.log(arr2);
4. for循环搭配includes
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22];
function noRepeat(arr) {
let newArr = [];
for(i=0; i<arr.length; i++){
if(!newArr.includes(arr[i])){
newArr.push(arr[i])
}
}
return newArr
}
console.log(noRepeat(arr));