javascript 从数组中删除重复元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16747798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:50:02  来源:igfitidea点击:

Delete duplicate elements from an array

javascriptarrays

提问by Mehmet Ince

For example, I have an array like this;

例如,我有一个这样的数组;

var arr = [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 10, 10]

My purpose is to discard repeating elements from array and get final array like this;

我的目的是丢弃数组中的重复元素并像这样获得最终数组;

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

How can this be achieved in JavaScript?

这如何在 JavaScript 中实现?

NOTE: array is not sorted, values can be arbitrary order.

注意:数组未排序,值可以是任意顺序。

回答by Niccolò Campolungo

It's easier using Array.filter:

使用更容易Array.filter

var unique = arr.filter(function(elem, index, self) {
    return index === self.indexOf(elem);
})

回答by Denys Séguret

As elements are yet ordered, you don't have to build a map, there's a fast solution :

由于元素尚未排序,因此您不必构建地图,有一个快速的解决方案:

var newarr = [arr[0]];
for (var i=1; i<arr.length; i++) {
   if (arr[i]!=arr[i-1]) newarr.push(arr[i]);
}

If your array weren't sorted, you would use a map :

如果您的数组未排序,您将使用 map :

var newarr = (function(arr){
  var m = {}, newarr = []
  for (var i=0; i<arr.length; i++) {
    var v = arr[i];
    if (!m[v]) {
      newarr.push(v);
      m[v]=true;
    }
  }
  return newarr;
})(arr);

Note that this is, by far, much faster than the accepted answer.

请注意,到目前为止,这比公认的答案要快得多。

回答by Kevin Bowersox

var arr = [1,2,2,3,4,5,5,5,6,7,7,8,9,10,10];

function squash(arr){
    var tmp = [];
    for(var i = 0; i < arr.length; i++){
        if(tmp.indexOf(arr[i]) == -1){
        tmp.push(arr[i]);
        }
    }
    return tmp;
}

console.log(squash(arr));

Working Examplehttp://jsfiddle.net/7Utn7/

工作示例http://jsfiddle.net/7Utn7/

Compatibility for indexOf on old browsers

indexOf 在旧浏览器上的兼容性

回答by sAnS

you may try like this using jquery

你可以试试这样使用 jquery

 var arr = [1,2,2,3,4,5,5,5,6,7,7,8,9,10,10];
    var uniqueVals = [];
    $.each(arr, function(i, el){
        if($.inArray(el, uniqueVals) === -1) uniqueVals.push(el);
    });

回答by Grijesh Chauhan

Try following from Removing duplicates from an Array(simple):

尝试从从数组中删除重复项(简单)

Array.prototype.removeDuplicates = 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;
} 

Edit:

编辑:

This code doesn't need sort:

此代码不需要排序:

Array.prototype.removeDuplicates = function (){
  var temp=new Array();
  label:for(i=0;i<this.length;i++){
        for(var j=0; j<temp.length;j++ ){//check duplicates
            if(temp[j]==this[i])//skip if already present 
               continue label;      
        }
        temp[temp.length] = this[i];
  }
  return temp;
 } 

(But not a tested code!)

但不是经过测试的代码!