- Set
var arr = [1,1,2,2,3,4,5,5,6,6,7,8,8,7]
function Arr(arr){
return Array.from(new Set(arr))
}
console.log(Arr(arr))
*[1,2,3,4,5,6,7,8]*
- indexof
var arr = [1,1,36,36,23,23,45,45]
function Arr(arr){
if(!Array.isArray(arr)){
console.log('type error!')
return
}
var bar = []
for(var i = 0;i < arr.length;i++){
if(bar.indexOf(arr[i])===-1){
bar.push(arr[i])
}
}
return bar;
}
console.log(Arr(arr))
* [1,36,23,45]*
- filter
var arr = [11,22,33,22,11,33,88,88,44,44]
function Arr(arr){
return arr.filter(function(item,index,arr){
return arr.indexOf(item,0)===index
})
}
console.log(Arr(arr))
* [11,22,33,88,44]*
- for
var arr = [12,13,12,13,14,14,15,16,15,16]
function Arr(arr){
for(var i = 0;i <arr.length;i++){
for(var j=i+1;j < arr.length;j++){
if(arr[i]==arr[j]){
arr.splice(j,1)
j--;
}
}
}
return arr
}
console.log(Arr(arr))
* [12,13,14,15,16]*