1、
function fun1(arr) {
var t = []
t[0] = arr[0]
for(let i = 0;i < arr.length;i++){
for(let k = 0;k < t.length;k++){
if(t[k] == arr[i])
break;
if(k == t.length - 1){
t.push(arr[i]);
}
}
}
return t
}
2、indexOf()
function fun2(arr) {
const temp = []
arr.forEach(e => {
if (temp.indexOf(e) == -1) {
temp.push(e)
}
})
return temp
}
e/details/103198884
3、filter()
function fun3(arr) {
var temp = arr.filter(function (element, index, self) {
return self.indexOf(element) === index;
})
return temp
}
4、Set()
function fun4(arr) {
return Array.from(new Set(arr))
}
5、includes()
Array.prototype.unique = function() {
const newArray = []
this.forEach = (item => {
if (!newArray.includes(item)) {
newArray.push(item)
}
})
return newArray
}
6、reduce(),此方法会先排序再去重
function fun6(arr) {
return arr.sort().reduce((init, current) => {
if (init.length === 0 || init[init.length - 1] !== current) {
init.push(current)
}
return init
}, [])
}
7、Map
function fun7(arr) {
const map = new Map()
return arr.filter(item => !map.has(item) && map.set(item, 1))
}