js

JS–原型上的数组去重

Array.prototype.remove = function () {
for (var i = 0; i < this.length; i++) {
if (this.indexOf(this[i]) !== this.lastIndexOf(this[i])) {
this.splice(i, 1);
i–;
}
}
return this;
};
console.log([1, 1, 2, 2, 2, 5, 5, 5, 4, 1, 2, 3].remove());

Array.prototype.remove=function () {
this==实例
var _this=this;
return this.filter(function (item,index) {
this==window
return !_this.slice(0,index).includes(item)
})
};
console.log([1, 1, 1, 1, 2, 2].remove());

利用对象的属性名不可以重复来实现数组去重
Array.prototype.remove=function () {
var obj={},ary=[];
this.forEach(function (item) {
obj[item]=item;
})
for (var key in obj){
key是字符串
ary.push(obj[key])
}
return ary;
}