Array.prototype.uniq = function () {
let len = this.length, f = true;
for (let i = 0; i< len; i++) {
if (this.indexOf(this[i]) !== i) {
if (f && isNaN(this[i])) {
f = false;
continue;
}
this.splice(i, 1);
i--;
len--;
}
}
return this;
}
let arr = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]
console.log("🚀 ~ file: 005 数组去重.js ~ line 13 ~ arr.uniq()", arr.uniq())
备注:这里需要注意一点: isNaN(undefined) 这个是true 我也不知道为啥
希望能帮助到您,点个赞啦,我的老北鼻