const dataList = [1, 2, 3, 4, 5, 2, 1, 3, 6];
// for循环嵌套去重
const forRemoveDuplicates = (arr) => {
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
if(arr[i] === arr[j]){
arr.splice(j, 1);
j--;
}
}
}
return arr;
};
console.log(forRemoveDuplicates(dataList));
// 利用filter去重
const filterRemoveDuplicates = (arr) => {
return arr.filter((item, index, self) => {
return self.indexOf(item, 0) === index;
})
};
console.log(filterRemoveDuplicates(dataList));
// 利用ES6 Set去重
const es6RemoveDuplicates = (arr) => {
return Array.from(new Set(arr));
};
console.log(es6RemoveDuplicates(dataList));
console