数组去重的常见写法:
var arr = [1,1,2,2,3,3]
var newArr = []
for(var i = 0; i < arr.length;i++){
if(newArr.indexOf(arr[i]) == -1 ){
newArr.push(arr[i])
}
}
console.log(newArr)
数组去重封装成方法:
function getInfo(arr){
var newArr = []
for(var i = 0; i < arr.length;i++){
if(newArr.indexOf(arr[i]) == -1 ){
newArr.push(arr[i])
}
}
console.log(newArr)
return arr
}
//调用:
var arr = [1,1,2,2,2]
getInfo(arr)
es6的数组去重(Array.from):
var arr = [1,1,12,3,1]
Array.from(new Set(arr))
console.log(Array.from(new Set(arr)))
网友评论