思路:先设定一个新的空数组,遍历给定数组,每次判断新数组中是否有这个元素,如果没有,则添加到新数组中。遍历完成后返回这个新数组。
const Arr=[8,4,1,2,3,2,4,5,6,1]
Array.prototype.noRepeat=function(){
let resArr=[]
for(let i=0;i<this.length;i++){
if(resArr.findIndex(v=>v==this[i])==-1){
resArr.push(this[i])
}
}
return resArr;//返回去重的新数组
}
let noRepeatArr=Arr.noRepeat()