JS - 给数组的原型添加去掉重复元素的distinct方法
/*
调用完该方法,原数组只留下非重复的数据
返回一个数组,里面是依次出现的重复元素
*/
Array.prototype.distinct = function () {
var removeArr = [], retainArr = [];
for (var i = 0; i < this.length; i ++) {
var elem = this[i];
if (this.indexOf(elem) !== i ) {
removeArr.push(elem);
}
else {
retainArr.push(elem);
}
}
this.splice(0);
for (var i = 0; i < retainArr.length; i ++) {
this.push(retainArr[i]);
}
return removeArr;
};