1、indexOf 判断元素是否存在数组中
let arr = [1,13,24,11,11,14,1,2]
function fn (arr) {
let arr1 = []
for (let i = 0; i < arr.length; i++) {
if (arr1.indexOf(arr[i]) === -1) {
arr1.push(arr[i])
}
}
return arr1
}
console.log(fn(arr)) //[1,2,11,14,13,24]
2、对象法
let arr = [1,13,24,11,11,14,1,2]
function fn1 (arr) {
let obj = {}
let data = []
for (let i = 0; i < arr.length; i++) {
if (!obj[arr[i]]) {
obj[arr[i]] = true
data.push(arr[i])
}
}
return data
}
console.log(fn1(arr)) //[1,2,11,14,13,24]