JS关于面试时数组去重最屌的写法--原型

 1 ~function () {
 2     var pro = Array.prototype;
 3     pro.myDistinct = function myDistinct() {
 4         var obj = {};
 5         for (var i = 0; i < this.length; i++) {
 6             var item = this[i];
 7             if(typeof obj[item]!== 'undefined'){
 8                 this[i] = this[this.length - 1];
 9                 this.length--;
10                 i--;
11                 continue;
12             }
13             obj[item] = item;
14         }
15         obj = null;
16         return this;
17     }
18 }();
19 var arr1 = [1,2,3,4,4,5,6,7,8,6];
20 console.log(arr1.myDistinct());

 

posted @ 2018-04-08 15:06  郑地豆  阅读(126)  评论(0编辑  收藏  举报