给数组原型对象添加一个去重的方法,并返回去重后的数组

 // 我的思路是采用哈希表来去重,最后将数组截取

 1 if ( !Array.prototype.unique ) {
 2         Array.prototype.unique = function () {
 3             var tmp = {},
 4                 arr = [],
 5                 j = 0,
 6                 i,
 7                 len;
 8             for (i = 0, len = this.length; i < len; i++) {
 9                 var value = this[i];
10                 if ( tmp[value] ) {
11                     arr.push(value);
12                 } else {
13                     this[j++] = value;
14                     tmp[value] = 1;
15                 }
16             }
17             this.length = j;
18             return arr;
19         }
20     }

原文地址:https://www.cnblogs.com/wuzy/p/3316121.html