标签:filter final 四种 ons repeat result lte for i++
// 第一种:// 第二种 O(2n)
function removeColone(arr){
let final = [];
for(let i = 0 ;i<arr.length;i++){
if(arr[i] !==arr[i+1]){
final.push(arr[i]);
}
}
console.log(final)
}
removeColone([1,2,2,3,3,4,4,5,5,5,5,7,8,9,9])
// 第三种
function rmoveProtype(arr){
let tem ={};
for(let i = 0;i<arr.length;i++){
tem[${arr[i]}
] = ‘a‘
}
let result=Object.keys(tem).map(res=>{
return parseInt(res)
})
return result;
}
//第四种includes() == -1
function removeRepeat(arr){
var tem = [];
for(let i = 0;i<arr.length;i++){
if(!arr.includes(arr[i])){
tem.push(arr[i])
}
}
console.log(tem)
}
//第五种filter
function removeRepeat(arr){
return arr.filter((currentValue,index,arr)=>{
return arr.indexOf(currentValue) ===index
})
}
console.log(removeRepeat([1,2,2,3,3,4,5,5,5,5]))
标签:filter final 四种 ons repeat result lte for i++
原文地址:https://blog.51cto.com/14582569/2511674