入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

js数组去重的常用方法总结

创建时间:2016-08-23 投稿人: 浏览次数:831

面试前端必须准备的一个问题:怎样去掉Javascript的Array的重复项。

这里总结了几种方法:



第一种是比较常规的方法

思路:

1.构建一个新的数组存放结果

2.for循环中每次从原数组中取出一个元素,用这个元素循环与结果数组对比

3.若结果数组中没有该元素,则存到结果数组中

代码如下:
Array.prototype.unique1 = function(){
var res = [this[0]];
for(var i = 1; i < this.length; i++){
var repeat = false;
for(var j = 0; j < res.length; j++){
if(this[i] == res[j]){
repeat = true;
break;
}
}
if(!repeat){
res.push(this[i]);
}
}
return res;
}
var arr = [1, "a", "a", "b", "d", "e", "e", 1, 0]
alert(arr.unique1());

第二种方法比上面的方法效率要高

思路:

1.先将原数组进行排序

2.检查原数组中的第i个元素 与 结果数组中的最后一个元素是否相同,因为已经排序,所以重复元素会在相邻位置

3.如果不相同,则将该元素存入结果数组中

代码如下:
Array.prototype.unique2 = function(){
this.sort(); //先排序
var res = [this[0]];
for(var i = 1; i < this.length; i++){
if(this[i] !== res[res.length - 1]){
res.push(this[i]);
}
}
return res;
}
var arr = [1, "a", "a", "b", "d", "e", "e", 1, 0]
alert(arr.unique2());

第二种方法也会有一定的局限性,因为在去重前进行了排序,所以最后返回的去重结果也是排序后的。如果要求不改变数组的顺序去重,那这种方法便不可取了。

第三种方法(推荐使用)

思路:

1.创建一个新的数组存放结果

2.创建一个空对象

3.for循环时,每次取出一个元素与对象进行对比,如果这个元素不重复,则把它存放到结果数组中,同时把这个元素的内容作为对象的一个属性,并赋值为1,存入到第2步建立的对象中。

说明:至于如何对比,就是每次从原数组中取出一个元素,然后到对象中去访问这个属性,如果能访问到值,则说明重复。

代码如下:
Array.prototype.unique3 = function(){
var res = [];
var json = {};
for(var i = 0; i < this.length; i++){
if(!json[this[i]]){
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
}

var arr = [112,112,34,"你好",112,112,34,"你好","str","str1"];
alert(arr.unique3());



第一种:也是最笨的吧。

Array.prototype.unique1 = function () {
    var r = new Array();
    label:for(var i = 0, n = this.length; i < n; i++) {
        for(var x = 0, y = r.length; x < y; x++) {
            if(r[x] == this[i]) {
                continue label;
            }
        }
        r[r.length] = this[i];
    }
    return r;
}


第二种:这个正则天书一样。

Array.prototype.unique2 = function () {
    return this.sort().join(",,").replace(/(,|^)([^,]+)(,,2)+(,|$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(",");
}



第三种:使用对象的【hasOwnProperty】方法

Array.prototype.unique3 = function() {
    var temp = {}, len = this.length;
    for(var i=0; i < len; i++)  {
        var tmp = this[i];
        if(!temp.hasOwnProperty(tmp)) {
            temp[this[i]] = "my god";
        }
    }
  
    len = 0;
    var tempArr=[];
    for(var i in temp) {
        tempArr[len++] = i;
    }
    return tempArr;
}
第四种:先排序,前项比后项。这个方法挺简单的,但也实用。


Array.prototype.unique4 = function () {
    var temp = new Array();
      this.sort();
      for(i = 0; i < this.length; i++) {
          if( this[i] == this[i+1]) {
            continue;
        }
          temp[temp.length]=this[i];
      }
      return temp;
  
}
 


下面是以前经常用的,效率也很好。有点想hash表的感觉。




Array.prototype.unique5 = function() {
    var res = [], hash = {};
    for(var i=0, elem; (elem = this[i]) != null; i++)  {
        if (!hash[elem])
        {
            res.push(elem);
            hash[elem] = true;
        }
    }
    return res;
}

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。