数组去重,地址不改变

183 阅读1分钟


var ary = [1,1,2,3,3,4,5,6,6];

Array.prototype.unique = function(){

let arr = [...new Set(this)]//实现去重

this.length = 0;//数组长度清零

this.push(...arr);//把去重的push进原数组

return this;//返回这个去重后的实例

}

console.log(ary.unique());