获取JavaScript数组中的所有唯一值(删除重复项)

时间:2009-12-25 04:28:24

标签: javascript arrays unique

我有一系列数字,我需要确保它们是唯一的。我在互联网上找到了下面的代码片段,它的工作情况很好,直到数组中的数字为零。我发现this other script在这里看起来几乎完全一样,但它不会失败。

所以为了帮助我学习,有人可以帮我确定原型脚本出错的地方吗?

Array.prototype.getUnique = function() {
 var o = {}, a = [], i, e;
 for (i = 0; e = this[i]; i++) {o[e] = 1};
 for (e in o) {a.push (e)};
 return a;
}

来自重复问题的更多答案:

类似的问题:

108 个答案:

答案 0 :(得分:2038)

使用 JavaScript 1.6 / ECMAScript 5 ,您可以按以下方式使用数组的本地filter方法来获取具有唯一值的数组:< / p>

function onlyUnique(value, index, self) { 
    return self.indexOf(value) === index;
}

// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']

本机方法filter将循环遍历数组,只留下那些通过给定回调函数onlyUnique的条目。

onlyUnique检查,如果给定的值是第一次发生。如果没有,则必须是重复的,不会被复制。

此解决方案无需任何额外的库,如jQuery或prototype.js。

它适用于具有混合值类型的数组。

对于不支持原生方法filterindexOf的旧浏览器(&lt; ie9),您可以在filter和{{3}的MDN文档中找到解决方法}。

如果您想保留最后一次出现的值,请简单地将indexOf替换为lastIndexOf

使用ES6可以缩短它:

// usage example:
var myArray = ['a', 1, 'a', 2, '1'];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i); 

// unique is ['a', 1, 2, '1']

感谢indexOf提供评论提示。

ES6有一个本机对象Camilo Martin来存储唯一值。要获得具有唯一值的数组,您现在可以这样做:

var myArray = ['a', 1, 'a', 2, '1'];

let unique = [...new Set(myArray)]; 

// unique is ['a', 1, 2, '1']

Set的构造函数接受一个可迭代的对象,如Array,并且扩展运算符...将该集合转换回一个数组。感谢Set提供评论提示。

答案 1 :(得分:688)

更新了ES6 / ES2015的答案:使用Set,单行解决方案是:

var items = [4,5,4,6,3,4,5,2,23,1,4,4,4]
var uniqueItems = Array.from(new Set(items))

返回

[4, 5, 6, 3, 2, 23, 1]

le_m所建议,也可以使用spread operator缩短,例如

var uniqueItems = [...new Set(items)]

答案 2 :(得分:127)

您也可以使用underscore.js

console.log(_.uniq([1, 2, 1, 3, 1, 4]));
<script src="http://underscorejs.org/underscore-min.js"></script>

将返回:

[1, 2, 3, 4]

答案 3 :(得分:122)

我意识到这个问题已有30多个答案。但我先阅读了所有现有的答案并进行了自己的研究。

我将所有答案分成4个可能的解决方案:

  1. 使用新的ES6功能:[...new Set( [1, 1, 2] )];
  2. 使用对象{ }来防止重复
  3. 使用帮助程序数组[ ]
  4. 使用filter + indexOf
  5. 以下是答案中的示例代码:

    使用新的ES6功能:[...new Set( [1, 1, 2] )];

    function uniqueArray0(array) {
      var result = Array.from(new Set(array));
      return result    
    }
    

    使用对象{ }来防止重复

    function uniqueArray1( ar ) {
      var j = {};
    
      ar.forEach( function(v) {
        j[v+ '::' + typeof v] = v;
      });
    
      return Object.keys(j).map(function(v){
        return j[v];
      });
    } 
    

    使用辅助数组[ ]

    function uniqueArray2(arr) {
        var a = [];
        for (var i=0, l=arr.length; i<l; i++)
            if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
                a.push(arr[i]);
        return a;
    }
    

    使用filter + indexOf

    function uniqueArray3(a) {
      function onlyUnique(value, index, self) { 
          return self.indexOf(value) === index;
      }
    
      // usage
      var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']
    
      return unique;
    }
    

    我想知道哪一个更快。我已经sample Google Sheet来测试函数了。注意:ECMA 6在Google表格中不可用,因此我无法对其进行测试。

    以下是测试结果: enter image description here

    我希望看到使用对象{ }的代码会赢,因为它使用哈希。所以我很高兴测试在Chrome和IE中显示了该算法的最佳结果。感谢@rab for the code

答案 4 :(得分:56)

One Liner,Pure JavaScript

使用ES6语法

echo "Launch excel" Set "ExcelArgs=CREO,DXFWITHOUTDRW "C:\Program Files (x86)\Microsoft Office\OFFICE16\Excel.exe" /r "%APPDATA%\Microsoft\Excel\XLSTART\PERSONAL.XLSB" exit 0

list = list.filter((x, i, a) => a.indexOf(x) == i)

enter image description here

使用ES5语法

x --> item in array
i --> index of item
a --> array reference, (in this case "list")

浏览器兼容性:IE9 +

答案 5 :(得分:51)

我发现了一个使用jQuery的好方法

arr = $.grep(arr, function(v, k){
    return $.inArray(v ,arr) === k;
});

注意:此代码是从Paul Irish's duck punching post中提取的 - 我忘了给予赞誉:P

答案 6 :(得分:46)

ES6的最短解决方案:[...new Set( [1, 1, 2] )];

或者如果你想修改数组原型(就像在原始问题中那样):

Array.prototype.getUnique = function() {
    return [...new Set( [this] )];
};
目前(2015年8月),现代浏览器中的EcmaScript 6仅为partially implemented,但Babel已经变得非常受欢迎,可以将ES6(甚至是ES7)转换回ES5。这样你今天就可以编写ES6代码了!

如果您想知道...的含义,那就称为spread operator。从MDN:«扩展运算符允许在需要多个参数(用于函数调用)或多个元素(用于数组文字)的位置扩展表达式»。因为Set是可迭代的(并且只能具有唯一值),所以扩展运算符将扩展Set以填充数组。

学习ES6的资源:

答案 7 :(得分:32)

最简单的解决方案:

&#13;
&#13;
var arr = [1, 3, 4, 1, 2, 1, 3, 3, 4, 1];
console.log([...new Set(arr)]);
&#13;
&#13;
&#13;

或者:

&#13;
&#13;
var arr = [1, 3, 4, 1, 2, 1, 3, 3, 4, 1];
console.log(Array.from(new Set(arr)));
&#13;
&#13;
&#13;

答案 8 :(得分:31)

最简单的fastest(以Chrome格式)执行此操作的方式:

Array.prototype.unique = function() {
    var a = [];
    for (var i=0, l=this.length; i<l; i++)
        if (a.indexOf(this[i]) === -1)
            a.push(this[i]);
    return a;
}

只需遍历数组中的每个项目,测试该项目是否已在列表中,如果不是,则推送到返回的数组。

根据jsPerf,这个函数是the fastest of the ones I could find anywhere - 随意添加你自己的函数。

非原型版本:

function uniques(arr) {
    var a = [];
    for (var i=0, l=arr.length; i<l; i++)
        if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
            a.push(arr[i]);
    return a;
}

排序

当还需要对数组进行排序时,以下是最快的:

Array.prototype.sortUnique = function() {
    this.sort();
    var last_i;
    for (var i=0;i<this.length;i++)
        if ((last_i = this.lastIndexOf(this[i])) !== i)
            this.splice(i+1, last_i-i);
    return this;
}

或非原型:

function sortUnique(arr) {
    arr.sort();
    var last_i;
    for (var i=0;i<arr.length;i++)
        if ((last_i = arr.lastIndexOf(arr[i])) !== i)
            arr.splice(i+1, last_i-i);
    return arr;
}

在大多数非Chrome浏览器中,这也是faster than the above method

答案 9 :(得分:29)

仅限表现!此代码可能比此处的所有代码快10倍 *适用于所有浏览器,并且内存影响最小.... 还有更多

如果您不需要重复使用旧数组;顺便说一下,在将其转换为唯一之前执行必要的其他操作可能是执行此操作的最快方法,也非常短。

var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];

然后你可以试试这个

&#13;
&#13;
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 1];

function toUnique(a, b, c) { //array,placeholder,placeholder
  b = a.length;
  while (c = --b)
    while (c--) a[b] !== a[c] || a.splice(c, 1);
  return a // not needed ;)
}
console.log(toUnique(array));
//[3, 4, 5, 6, 7, 8, 9, 0, 2, 1]
&#13;
&#13;
&#13;

我想出了这个函数阅读这篇文章......

http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/

我不喜欢for循环。它有许多参数。我喜欢while--循环。 虽然是所有浏览器中最快的循环,除了我们都非常喜欢的那个... chrome。

无论如何我编写了第一个使用while的函数。是的,它比文章中的函数快一点。但还不够。unique2()

下一步使用现代js。Object.keys 我用js1.7&#39;的Object.keys替换了另一个for循环... 更快更短(镀铬速度快2倍);)。还不够!unique3()

此时我正在思考我在我独特的功能中真正需要什么。 我不需要旧的阵列,我想要一个快速的功能。 所以我使用2 while while + splice。unique4()

无话可说,我印象深刻。

chrome:通常每秒150,000次操作跃升至每秒1,800,000次操作。

ie: 80,000 op / s vs 3,500,000 op / s

ios: 18,000 op / s vs 170,000 op / s

safari: 80,000 op / s vs 6,000,000 op / s

<强>证明 http://jsperf.com/wgu或更好地使用console.time ... microtime ......无论

unique5()只是为了向您展示如果您想要保留旧数组会发生什么。

如果您不知道自己在做什么,请不要使用Array.prototype。 我刚做了很多副本和过去。 如果要创建原生prototype.example,请使用Object.defineProperty(Array.prototype,...,writable:false,enumerable:false})https://stackoverflow.com/a/20463021/2450730

<强>演示 http://jsfiddle.net/46S7g/

注意:此操作后,您的旧阵列将被销毁/成为唯一。

如果你不能阅读上面的代码,请阅读javascript书或这里有一些关于更短代码的解释。 https://stackoverflow.com/a/21353032/2450730

有些人正在使用indexOf ...不要...... http://jsperf.com/dgfgghfghfghghgfhgfhfghfhgfh

表示空数组

!array.length||toUnique(array); 

答案 10 :(得分:26)

这里的许多答案可能对初学者没用。如果难以对数组进行重复数据删除,他们真的会知道原型链,甚至是jQuery吗?

在现代浏览器中,一个干净而简单的解决方案是将数据存储在Set中,该other ways设计为唯一值列表。

const cars = ['Volvo', 'Jeep', 'Volvo', 'Lincoln', 'Lincoln', 'Ford'];
const uniqueCars = Array.from(new Set(cars));

Array.from对于将Set转换回Array非常有用,这样您就可以轻松访问数组所具有的所有令人敬畏的方法(功能)。还有forEach做同样的事情。但你可能根本不需要Array.from,因为集合有很多有用的功能,例如{{3}}。

如果您需要支持旧的Internet Explorer,因此无法使用Set,那么一种简单的方法是将项目复制到新阵列,同时事先检查它们是否已经在新阵列中。

// Create a list of cars, with duplicates.
var cars = ['Volvo', 'Jeep', 'Volvo', 'Lincoln', 'Lincoln', 'Ford'];
// Create a list of unique cars, to put a car in if we haven't already.
var uniqueCars = [];

// Go through each car, one at a time.
cars.forEach(function (car) {
    // The code within the following block runs only if the
    // current car does NOT exist in the uniqueCars list
    // - a.k.a. prevent duplicates
    if (uniqueCars.indexOf(car) === -1) {
        // Since we now know we haven't seen this car before,
        // copy it to the end of the uniqueCars list.
        uniqueCars.push(car);
    }
});

为了使其立即可重复使用,我们将其置于一个功能中。

function deduplicate(data) {
    if (data.length > 0) {
        var result = [];

        data.forEach(function (elem) {
            if (result.indexOf(elem) === -1) {
                result.push(elem);
            }
        });

        return result;
    }
}

因此,为了摆脱重复,我们现在就这样做。

var uniqueCars = deduplicate(cars);

deduplicate(cars)部分变为,当函数完成时,我们将 result 命名为

只需传递你喜欢的任何数组的名称。

答案 11 :(得分:20)

["Defects", "Total", "Days", "City", "Defects"].reduce(function(prev, cur) {
  return (prev.indexOf(cur) < 0) ? prev.concat([cur]) : prev;
 }, []);

[0,1,2,0,3,2,1,5].reduce(function(prev, cur) {
  return (prev.indexOf(cur) < 0) ? prev.concat([cur]) : prev;
 }, []);

答案 12 :(得分:17)

这个原型getUnique并不完全正确,因为如果我有一个像["1",1,2,3,4,1,"foo"]这样的数组,它会返回["1","2","3","4"]"1"就是字符串而1是整数;他们是不同的。

这是一个正确的解决方案:

Array.prototype.unique = function(a){
    return function(){ return this.filter(a) }
}(function(a,b,c){ return c.indexOf(a,b+1) < 0 });

使用:

var foo;
foo = ["1",1,2,3,4,1,"foo"];
foo.unique();

上述内容将产生["1",2,3,4,1,"foo"]

答案 13 :(得分:15)

我们可以使用ES6套装来实现这一目标:

&#13;
&#13;
var duplicatedArray = [1, 2, 3, 4, 5, 1, 1, 1, 2, 3, 4];
var uniqueArray = Array.from(new Set(duplicatedArray));

console.log(uniqueArray);
&#13;
&#13;
&#13;

//输出将是

uniqueArray = [1,2,3,4,5];

答案 14 :(得分:14)

您可以使用Set运算符从数组中获取唯一值

const uniqueArray = [...new Set([1, 1, 1])];

console.log(uniqueArray) // [1]

答案 15 :(得分:12)

如果不扩展Array.prototype(据说这是一种不好的做法)或使用jquery / underscore,你可以简单地filter数组。

保持最后一次出现:

    function arrayLastUnique(array) {
        return array.filter(function (a, b, c) {
            // keeps last occurrence
            return c.indexOf(a, b + 1) < 0;
        });
    },

或第一次出现:

    function arrayFirstUnique(array) {
        return array.filter(function (a, b, c) {
            // keeps first occurrence
            return c.indexOf(a) === b;
        });
    },

嗯,它只是javascript ECMAScript 5+,这意味着只有IE9 +,但它适用于原生HTML / JS(Windows应用商店应用,Firefox OS,Sencha,Phonegap,Titanium等)的开发。

答案 16 :(得分:10)

那是因为0是JavaScript中的假值。

如果数组的值为0或任何其他假值,则

this[i]将是假的。

答案 17 :(得分:10)

Array.prototype.getUnique = function() {
    var o = {}, a = []
    for (var i = 0; i < this.length; i++) o[this[i]] = 1
    for (var e in o) a.push(e)
    return a
}

答案 18 :(得分:10)

如果你正在使用Prototype框架,则不需要执行'for'循环,你可以像这样使用http://www.prototypejs.org/api/array/uniq

var a = Array.uniq();  

这将生成一个没有重复的重复数组。我遇到了一个问题,在

之后搜索一个计算不同数组记录的方法
  

的uniq()

我用过

  

尺寸()

我的结果很简单。 附:对不起,如果我错误输入了什么

编辑:如果你想要转义未定义的记录,你可能想要添加

  

compact()

之前,像这样:

var a = Array.compact().uniq();  

答案 19 :(得分:9)

最简单的答案是:

const array = [1, 1, 2, 2, 3, 5, 5, 2];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // [1, 2, 3, 5]

答案 20 :(得分:9)

在这里查看了所有 90 + 个答案之后,我发现还有一个余地:

Array.includes有一个非常方便的第二个参数:“ fromIndex” ,因此通过使用它,filter回调方法的每次迭代都将搜索数组,从[current index] + 1开始,保证不在查询中包含当前已过滤的项目,而且还节省了时间。

//                ?              ? ?
var list = [0,1,2,2,3,'a','b',4,5,2,'a']

console.log( 
  list.filter((v,i) => !list.includes(v,i+1))
)

// [0,1,3,"b",4,5,2,"a"]

说明:

例如,假设filter函数当前正在索引2处进行迭代,并且该索引处的值恰好是2。然后在数组部分中扫描重复项(includes方法)是在索引2(i+1)之后的所有内容:

           ?                    ?
[0, 1, 2,   2 ,3 ,'a', 'b', 4, 5, 2, 'a']
       ?   |---------------------------|

并且由于当前已过滤项的值2包含在数组的其余部分中,因此它会被过滤掉,因为前导的感叹号会否定过滤规则。

答案 21 :(得分:8)

魔术

a.filter(e=>!(t[e]=e in t)) 

O(n) performance;我们假设您的数组位于at={}中。说明here(+ Jeppe展示)

let t={}, unique= a=> a.filter(e=>!(t[e]=e in t));

// "stand-alone" version working with global t:
// a1.filter((t={},e=>!(t[e]=e in t)));

// Test data
let a1 = [5,6,0,4,9,2,3,5,0,3,4,1,5,4,9];
let a2 = [[2, 17], [2, 17], [2, 17], [1, 12], [5, 9], [1, 12], [6, 2], [1, 12]];
let a3 = ['Mike', 'Adam','Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl'];

// Results
console.log(JSON.stringify( unique(a1) ))
console.log(JSON.stringify( unique(a2) ))
console.log(JSON.stringify( unique(a3) ))

答案 22 :(得分:6)

我不确定为什么加布里埃尔·西尔维拉(Gabriel Silveira)以这种方式编写了这个函数,但是一个对我来说同样适用的简单形式也没有缩小:

Array.prototype.unique = function() {
  return this.filter(function(value, index, array) {
    return array.indexOf(value, index + 1) < 0;
  });
};

或在CoffeeScript中:

Array.prototype.unique = ->
  this.filter( (value, index, array) ->
    array.indexOf(value, index + 1) < 0
  )

答案 23 :(得分:6)

以简单方法

查找唯一的数组值
function arrUnique(a){
  var t = [];
  for(var x = 0; x < a.length; x++){
    if(t.indexOf(a[x]) == -1)t.push(a[x]);
  }
  return t;
}
arrUnique([1,4,2,7,1,5,9,2,4,7,2]) // [1, 4, 2, 7, 5, 9]

答案 24 :(得分:6)

奇怪的是,之前没有建议..要删除数组中的对象键(id下面)的重复项,你可以这样做:

const uniqArray = array.filter((obj, idx, arr) => (
  arr.findIndex((o) => o.id === obj.id) === idx
)) 

答案 25 :(得分:6)

我有一个稍微不同的问题,我需要从数组中删除具有重复id属性的对象。这行得通。

let objArr = [{
  id: '123'
}, {
  id: '123'
}, {
  id: '456'
}];

objArr = objArr.reduce((acc, cur) => [
  ...acc.filter((obj) => obj.id !== cur.id), cur
], []);

console.log(objArr);

答案 26 :(得分:5)

Shamasis Bhattacharya's blog(O(2n)时间复杂度):

Array.prototype.unique = function() {
    var o = {}, i, l = this.length, r = [];
    for(i=0; i<l;i+=1) o[this[i]] = this[i];
    for(i in o) r.push(o[i]);
    return r;
};

来自Paul Irish's blog:改进JQuery .unique()

(function($){

    var _old = $.unique;

    $.unique = function(arr){

        // do the default behavior only if we got an array of elements
        if (!!arr[0].nodeType){
            return _old.apply(this,arguments);
        } else {
            // reduce the array to contain no dupes via grep/inArray
            return $.grep(arr,function(v,k){
                return $.inArray(v,arr) === k;
            });
        }
    };
})(jQuery);

// in use..
var arr = ['first',7,true,2,7,true,'last','last'];
$.unique(arr); // ["first", 7, true, 2, "last"]

var arr = [1,2,3,4,5,4,3,2,1];
$.unique(arr); // [1, 2, 3, 4, 5]

答案 27 :(得分:5)

要以相反的方式解决问题,在加载数组时没有重复可能很有用,Set对象的方式可以做到,但它还没有在所有浏览器中都可用。如果您需要多次查看其内容,它可以节省内存并提高效率。

Array.prototype.add = function (elem) {
   if (this.indexOf(elem) == -1) {
      this.push(elem);
   }
}

样品:

set = [];
[1,3,4,1,2,1,3,3,4,1].forEach(function(x) { set.add(x); });

给你set = [1,3,4,2]

答案 28 :(得分:5)

基于ES6的解决方案...

var arr = [2, 3, 4, 2, 3, 4, 2];
const result = [...new Set(arr)];
console.log(result);

答案 29 :(得分:5)

您也可以使用 Array.from 方法将 Set 转换为 Array,如果接受的答案不适用于 Typescript。

如果出现错误: Type 'Set<any>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.ts(2569)

let uniques = Array.from(new Set([1, 2, 3, 1, 1])) ;
console.log(uniques);

答案 30 :(得分:4)

如果您对额外的依赖项没有问题,或者您的代码库中已经有一个库,则可以使用LoDash(或Underscore)从阵列中删除重复项。

<强>用法

如果您已经没有在代码库中使用它,请使用npm:

进行安装
npm install lodash

然后按如下方式使用:

import _ from 'lodash';
let idArray = _.uniq ([
    1,
    2,
    3,
    3,
    3
]);
console.dir(idArray);

Out:

[ 1, 2, 3 ]

答案 31 :(得分:4)

这个问题已经得到了很多回答,但是没有满足我的特殊需求。

许多答案是这样的:

a.filter((item, pos, self) => self.indexOf(item) === pos);

但这不适用于复杂对象的数组。

说我们有一个像这样的数组:

const a = [
 { age: 4, name: 'fluffy' },
 { age: 5, name: 'spot' },
 { age: 2, name: 'fluffy' },
 { age: 3, name: 'toby' },
];

如果我们希望对象具有唯一的名称,则应使用array.prototype.findIndex而不是array.prototype.indexOf

a.filter((item, pos, self) => self.findIndex(v => v.name === item.name) === pos);

答案 32 :(得分:4)

对于具有一些唯一ID的基于对象的数组,我有一个简单的解决方案,通过它您可以按线性复杂度进行排序

ListT(List(List(0, 1, 0, 0, 1), List(0, 1, 1, 0, 1), List(0, 1, 0, 0), List(0, 1, 0, 1), List(0, 1, 1, 0), List(0, 1, 1, 1)))
ListT(List(List(0, 1, 0, 0, 1), List(0, 1, 0, 0), List(0, 1, 0, 1), List(0, 1, 1, 0, 1), List(0, 1, 1, 0), List(0, 1, 1, 1)))

答案 33 :(得分:4)

只有一行代码:

    var arr = [1, 1, 2, 3] // example 
    var unique = new Set(arr) // 1, 2, 3
    console.log(unique) // 1, 2, 3 when I run in node.js

答案 34 :(得分:3)

如果有人使用knockoutjs

ko.utils.arrayGetDistinctValues()

BTW已经查看了所有ko.utils.array*实用程序。

答案 35 :(得分:3)

我发现序列化他们的哈希键帮助我实现了对象的工作。

Array.prototype.getUnique = function() {
        var hash = {}, result = [], key; 
        for ( var i = 0, l = this.length; i < l; ++i ) {
            key = JSON.stringify(this[i]);
            if ( !hash.hasOwnProperty(key) ) {
                hash[key] = true;
                result.push(this[i]);
            }
        }
        return result;
    }

答案 36 :(得分:3)

您也可以使用sugar.js:

[1,2,2,3,1].unique() // => [1,2,3]

[{id:5, name:"Jay"}, {id:6, name:"Jay"}, {id: 5, name:"Jay"}].unique('id') 
  // => [{id:5, name:"Jay"}, {id:6, name:"Jay"}]

答案 37 :(得分:3)

我认为这是从数组中获取唯一项的最简单方法。

var arr = [1,2,4,1,4];
arr = Array.from(new Set(arr))
console.log(arr)

答案 38 :(得分:3)

使用set怎么样?

   let productPrice = [230,560,125,230,678,45,230,125,127];

   let tempData = new Set(productPrice);
   let uniqeProductPrice = [...tempData];

   uniqeProductPrice.forEach((item)=>{
      console.log(item)
    });

答案 39 :(得分:3)

任务是从由任意类型(原始和非原始)组成的数组中获取唯一数组。

基于使用new Map(...)的方法并不新鲜。在这里,它由JSON.stringify(...)JSON.parse(...)[].map方法利用。优点是通用性(适用于任何类型的数组),简短的ES6表示法和在这种情况下可能具有性能

const dedupExample = [
    { a: 1 },
    { a: 1 },
    [ 1, 2 ],
    [ 1, 2 ],
    1,
    1,
    '1',
    '1'
]

const getUniqArr = arr => {
    const arrStr = arr.map(item => JSON.stringify(item))
    return [...new Set(arrStr)]
        .map(item => JSON.parse(item))
}

console.info(getUniqArr(dedupExample))
   /* [ {a: 1}, [1, 2], 1, '1' ] */

答案 40 :(得分:3)

这是一个几乎是 O(n) 的单行代码,保留第一个元素,并且您可以在其中保持您正在单独使用的字段。

这是函数式编程中非常常见的技术 - 您使用 reduce 构建返回的数组。由于我们像这样构建数组,我们保证我们得到稳定的排序,这与 [...new Set(array)] 方法不同。我们仍然使用 Set 来确保我们没有重复项,因此我们的累加器同时包含 Set 和我们正在构建的数组。

const removeDuplicates = (arr) =>
  arr.reduce(
    ([set, acc], item) => set.has(item) ? [set, acc] : [set.add(item), (acc.push(item), acc)],
    [new Set(), []]
  )[1]

以上内容适用于简单值,但不适用于对象,类似于 [...new Set(array)] 分解的方式。如果项目是包含 id 属性的对象,您应该这样做:

const removeDuplicates = (arr) =>
  arr.reduce(
    ([set, acc], item) => set.has(item.id) ? [set, acc] : [set.add(item.id), (acc.push(item), acc)],
    [new Set(), []]
  )[1]

答案 41 :(得分:3)

对于 TS 用户!

const uniq = <T>(arr: T[]) => [...new Set(arr)];

答案 42 :(得分:2)

您也可以使用jQuery

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

// note: jQuery's filter params are opposite of javascript's native implementation :(
var unique = $.makeArray($(a).filter(function(i,itm){ 
    // note: 'index', not 'indexOf'
    return i == $(a).index(itm);
}));

// unique: [1, 5, 6, 4, 2, 3]

最初回答:jQuery function to get all unique elements from an array?

答案 43 :(得分:2)

这样可行。

function getUnique(a) {
  var b = [a[0]], i, j, tmp;
  for (i = 1; i < a.length; i++) {
    tmp = 1;
    for (j = 0; j < b.length; j++) {
      if (a[i] == b[j]) {
        tmp = 0;
        break;
      }
    }
    if (tmp) {
      b.push(a[i]);
    }
  }
  return b;
}

答案 44 :(得分:2)

在其他答案的基础上,这是另一个变量,它采用可选标志来选择策略(保留第一次出现或保持最后一次):

不延伸 Array.prototype

function unique(arr, keepLast) {
  return arr.filter(function (value, index, array) {
    return keepLast ? array.indexOf(value, index + 1) < 0 : array.indexOf(value) === index;
  });
};

// Usage
unique(['a', 1, 2, '1', 1, 3, 2, 6]); // -> ['a', 1, 2, '1', 3, 6]
unique(['a', 1, 2, '1', 1, 3, 2, 6], true); // -> ['a', '1', 1, 3, 2, 6]

扩展 Array.prototype

Array.prototype.unique = function (keepLast) {
  return this.filter(function (value, index, array) {
    return keepLast ? array.indexOf(value, index + 1) < 0 : array.indexOf(value) === index;
  });
};

// Usage
['a', 1, 2, '1', 1, 3, 2, 6].unique(); // -> ['a', 1, 2, '1', 3, 6]
['a', 1, 2, '1', 1, 3, 2, 6].unique(true); // -> ['a', '1', 1, 3, 2, 6]

答案 45 :(得分:2)

使用对象键创建唯一数组,我尝试了以下

function uniqueArray( ar ) {
  var j = {};

  ar.forEach( function(v) {
    j[v+ '::' + typeof v] = v;
  });


  return Object.keys(j).map(function(v){
    return j[v];
  });
}   

uniqueArray(["1",1,2,3,4,1,"foo", false, false, null,1]);

返回["1", 1, 2, 3, 4, "foo", false, null]

答案 46 :(得分:2)

看看这个。 Jquery提供uniq方法: https://api.jquery.com/jQuery.unique/

var ids_array = []

$.each($(my_elements), function(index, el) {
    var id = $(this).attr("id")
    ids_array.push(id)
});

var clean_ids_array = jQuery.unique(ids_array)

$.each(clean_ids_array, function(index, id) {
   elment = $("#" + id)   // my uniq element
   // TODO WITH MY ELEMENT
});

答案 47 :(得分:2)

如果你有强大的reduce方法(≥ 5.1),你可以尝试这样的事情:

fmt.Fprintf(rw, "%+v\n", *m)

它不是最有效的实现(因为Array.prototype.uniq = function() { return this.reduce(function(sofar, cur) { return sofar.indexOf(cur) < 0 ? sofar.concat([cur]) : sofar; }, []); }; 检查,在最坏的情况下可能会通过整个列表)。如果效率很重要,你可以保持历史记录&#34;在一些随机访问结构(例如,indexOf)中发生的事件并改为关键。这基本上就是most voted answer的作用,所以请查看一下这个例子。

答案 48 :(得分:2)

使用 lodash 和身份lambda函数进行操作,只需在使用对象之前对其进行定义

const _ = require('lodash');
...    
_.uniqBy([{a:1,b:2},{a:1,b:2},{a:1,b:3}], v=>v.a.toString()+v.b.toString())
_.uniq([1,2,3,3,'a','a','x'])

并将具有:

[{a:1,b:2},{a:1,b:3}]
[1,2,3,'a','x']

(这是最简单的方法)

答案 49 :(得分:2)

单行显示纯js:

const uniqueArray = myArray.filter((elem, pos) => myArray.indexOf(elem) == pos); 

答案 50 :(得分:2)

postChanghui Xu,为那些想要获得独特对象的人创建了这个甜美的片段。不过,我尚未将其性能与其他选择进行比较。

const array = [{
    name: 'Joe',
    age: 17
  },
  {
    name: 'Bob',
    age: 17
  },
  {
    name: 'Tom',
    age: 25
  },
  {
    name: 'John',
    age: 22
  },
  {
    name: 'Jane',
    age: 20
  },
];

const distinctAges = [...new Set(array.map(a => a.age))];

console.log(distinctAges)

答案 51 :(得分:2)

使用以下方法从数组中删除重复元素的多种方法

  • 设置
  • 过滤器
  • forEach
  • lodash(第三方库)
  • for循环

// Pseudo code
create row of size (sum+1) 

row[0] = 1 // base condition

fill rest of the row with zeros

for element in arr:   /* for (int i = 0; i < arr.length; i++) */
    from column j where j - element >= 0 to end of row /* int j = arr[i]; j <= sum; j++ */
    row[j] += row[j-element]

return last element of row

使用lodash

const names = ['XYZ', 'ABC', '123', 'ABC', 'ACE', 'ABC', '123'];

// set
let unique = [...new Set(names)];
console.log(unique);

// filter
let x = (names) => names.filter((val,i) => names.indexOf(val) === i)
console.log(x(names));


// forEach
function removeDuplicates(names) {
  let unique = {};
  names.forEach(function(i) {
    if(!unique[i]) {
      unique[i] = true;
    }
  });
  return Object.keys(unique);
}

console.log(removeDuplicates(names));

代码

npm i lodash

使用简单的for循环

import _ from 'lodash';

let uniqueVal = _.uniq (names);
console.dir(uniqueVal);

答案 52 :(得分:2)

使用过滤器方法查找唯一元素的最简单方法:

var A1 = [2, 2, 4, 5, 5, 6, 8, 8, 9];

var uniqueA1 = A1.filter(function(element) {
  return A1.indexOf(element) == A1.lastIndexOf(element);
});

console.log(uniqueA1); // Output: [4,6,9]

这里的逻辑是,元素的第一次出现和最后一次出现是相同的,然后此元素仅出现一次,因此,通过回调条件的数组元素将包含在新数组中并显示。

答案 53 :(得分:1)

Array.prototype.unique = function() {
    var a = [],k = 0,e;
    for(k=0;e=this[k];k++)
      if(a.indexOf(e)==-1)
           a.push(e);
    return a;
}
[1,2,3,4,33,23,2,3,22,1].unique(); // return [1,2,3,4,33,23,22]

答案 54 :(得分:1)

另一种解决方案。

我最近需要将排序列表设为唯一,我使用过滤器来跟踪上一个项目,如下所示:

uniqueArray = sortedArray.filter(function(e) { 
    if(e==this.last) 
      return false; 
    this.last=e; return true;  
  },{last:null});

答案 55 :(得分:1)

接受选择器的版本应该非常快速和简洁:

function unique(xs, f) {
  var seen = {};
  return xs.filter(function(x) {
    var fx = (f && f(x)) || x;
    return !seen[fx] && (seen[fx] = 1);
  });
}

答案 56 :(得分:1)

这个不纯,它会修改数组,但这是最快的一个。如果你的速度更快,请写下评论;)

http://jsperf.com/unique-array-webdeb

Array.prototype.uniq = function(){
    for(var i = 0, l = this.length; i < l; ++i){
        var item = this[i];
        var duplicateIdx = this.indexOf(item, i + 1);
        while(duplicateIdx != -1) {
            this.splice(duplicateIdx, 1);
            duplicateIdx = this.indexOf(item, duplicateIdx);
            l--;
        }
    }

    return this;
}

[
 "",2,4,"A","abc",
 "",2,4,"A","abc",
 "",2,4,"A","abc",
 "",2,4,"A","abc",
 "",2,4,"A","abc",
 "",2,4,"A","abc",
 "",2,4,"A","abc",
 "",2,4,"A","abc"
].uniq() //  ["",2,4,"A","abc"]

答案 57 :(得分:1)

我知道这已经被解决了......但...... 没有人提到linq的javascript实现。 然后可以使用.distinct()方法 - 它使代码非常容易阅读。

var Linq = require('linq-es2015');
var distinctValues =  Linq.asEnumerable(testValues)
            .Select(x)
            .distinct()
            .toArray();

&#13;
&#13;
var testValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 1];

var distinctValues = Enumerable.asEnumerable(testValues)
  .distinct()
  .toArray();

console.log(distinctValues);
&#13;
<script src="https://npmcdn.com/linq-es5/dist/linq.js"></script>
&#13;
&#13;
&#13;

答案 58 :(得分:1)

这是一个ES6函数,可以从对象数组中删除重复项,按指定的对象属性进行过滤

function dedupe(arr = [], fnCheck = _ => _) {
  const set = new Set();
  let len = arr.length;

  for (let i = 0; i < len; i++) {
    const primitive = fnCheck(arr[i]);
    if (set.has(primitive)) {
      // duplicate, cut it
      arr.splice(i, 1);
      i--;
      len--;
    } else {
      // new item, add it
      set.add(primitive);
    }
  }

  return arr;
}

const test = [
    {video:{slug: "a"}},
    {video:{slug: "a"}},
    {video:{slug: "b"}},
    {video:{slug: "c"}},
    {video:{slug: "c"}}
]
console.log(dedupe(test, x => x.video.slug));

// [{video:{slug: "a"}}, {video:{slug: "b"}}, {video:{slug: "c"}}]

答案 59 :(得分:1)

与@sergeyz解决方案类似,但通过使用更多的简写格式(如箭头函数和array.includes)更加紧凑。 警告:由于使用逻辑或逗号,JSlint会抱怨。(尽管仍然是完全有效的javascript)

my_array.reduce((a,k)=>(a.includes(k)||a.push(k),a),[])

答案 60 :(得分:1)

您可以使用Ramda.js(一种功能强大的javascript库)来完成此操作:

var unique = R.uniq([1, 2, 1, 3, 1, 4])
console.log(unique)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

答案 61 :(得分:1)

我有一个使用es6 reduce并找到数组助手方法来删除重复项的解决方案。

let numbers = [2, 2, 3, 3, 5, 6, 6];

const removeDups = array => {
  return array.reduce((acc, inc) => {
    if (!acc.find(i => i === inc)) {
      acc.push(inc);
    }
    return acc;
  }, []);
}

console.log(removeDups(numbers)); /// [2,3,5,6]

答案 62 :(得分:1)

[
{
    "name":"Paul",
    "telephone": 5434523542,
    "email": "paul@xd.com"
},
{
    "name":"William",
    "telephone": 23423520,
    "email": "aijda@fns.com"
}
]

答案 63 :(得分:1)

重复数据删除通常需要给定类型的相等运算符。但是,使用eq函数会使我们无法有效地利用Set来确定重复项,因为Set会退回到===。如您所知,===对于引用类型无效。因此,如果遇到困难,我们会很友善吧?

解决方法只是使用转换函数,该函数允许我们将(引用)类型转换为实际上可以使用Set查找的内容。例如,我们可以使用哈希函数,或者使用JSON.stringify数据结构(如果其中不包含任何函数)。

通常,我们只需要访问一个属性,然后可以将其进行比较,而不是Object的引用。

这里有两个符合这些要求的组合器:

const dedupeOn = k => xs => {
  const s = new Set();

  return xs.filter(o =>
    s.has(o[k])
      ? null
      : (s.add(o[k]), o[k]));
};

const dedupeBy = f => xs => {
  const s = new Set();

  return xs.filter(x => {
    const r = f(x);
    
    return s.has(r)
      ? null
      : (s.add(r), x);
  });
};

const xs = [{foo: "a"}, {foo: "b"}, {foo: "A"}, {foo: "b"}, {foo: "c"}];

console.log(
  dedupeOn("foo") (xs)); // [{foo: "a"}, {foo: "b"}, {foo: "A"}, {foo: "c"}]

console.log(
  dedupeBy(o => o.foo.toLowerCase()) (xs)); // [{foo: "a"}, {foo: "b"}, {foo: "c"}]

使用这些组合器,我们可以非常灵活地处理各种重复数据删除问题。这不是斋戒方法,而是最具表现力和最通用的方法。

答案 64 :(得分:1)

减少它!

此替代方法不是显式地进行重复数据删除,而是采用数组并减少数组,以便可以累积的方式对数组的每个值进行迭代和解构,因为递归性,通过利用数组的持久性来忽略已经包含的值

['a', 1, 'a', 2, '1'].reduce((accumulator, currentValue) => accumulator.includes(currentValue) ? accumulator : [...accumulator, currentValue], [])

测试示例:

var array = ['a', 1, 'a', 2, '1'];
const reducer = (accumulator, currentValue) => accumulator.includes(currentValue) ? accumulator : [...accumulator, currentValue];

console.log(
  array.reduce(reducer, [])
);

结论

希望避免无聊的for-each方法时更加优雅和有用(不是无用)。

无需像Underscore.js,JQuery或Lo-Dash这样的外部库,也无需创建任何内置函数来实现所需的重复数据删除效果。

哦,嘿!,它可以单线完成!!!


感谢ES5(ECMAScript 2015)include()reduce(),此答案才得以实现。

答案 65 :(得分:1)

与具有list.indexOf的变体相比,它应该具有更好的性能

function uniq(list) { return [...new Set(list)] }

答案 66 :(得分:1)

使用“一线”在对象数组中查找唯一

const uniqueBy = (x,f)=>Object.values(x.reduce((a,b)=>((a[f(b)]=b),a),{}));
// f -> should must return string because it will be use as key

const data = [
  { comment: "abc", forItem: 1, inModule: 1 },
  { comment: "abc", forItem: 1, inModule: 1 },
  { comment: "xyz", forItem: 1, inModule: 2 },
  { comment: "xyz", forItem: 1, inModule: 2 },
];

uniqueBy(data, (x) => x.forItem +'-'+ x.inModule); // find unique by item with module
// output
// [
//   { comment: "abc", forItem: 1, inModule: 1 },
//   { comment: "xyz", forItem: 1, inModule: 2 },
// ];

// can also use for strings and number or other primitive values

uniqueBy([1, 2, 2, 1], (v) => v); // [1, 2]
uniqueBy(["a", "b", "a"], (v) => v); // ['a', 'b']

uniqueBy(
  [
    { id: 1, name: "abc" },
    { id: 2, name: "xyz" },
    { id: 1, name: "abc" },
  ],
  (v) => v.id
);
// output
// [
//   { id: 1, name: "abc" },
//   { id: 2, name: "xyz" },
// ];

答案 67 :(得分:1)

我使用这个极端简单的,我很确定它与所有浏览器兼容,但它在现代浏览器中已经过时:

var keys = [1,3,3,3,3,2,2,1,4,4,'aha','aha'];
for (var uniqueIdx = 0; uniqueIdx < keys.length; uniqueIdx++) {
  for (var duplicateCandidateIdx=0; duplicateCandidateIdx < keys.length; duplicateCandidateIdx++) {
    if(uniqueIdx != duplicateCandidateIdx) {
      var candidateIsFullDuplicate = keys[uniqueIdx] == keys[duplicateCandidateIdx];
      if (candidateIsFullDuplicate){
        keys.splice(duplicateCandidateIdx, 1);
      }
    }
  }
}
document.write(keys); // gives: 1,3,2,4,aha

答案 68 :(得分:1)

一些性能比较:

let to_filter = [...Array(100000000)].map(_ => Math.floor(Math.random()*100))
let unique_1 = arr => {
  const seen = {}
  const out = []
  for (let i = 0, v; i < arr.length; i++) {
    v = arr[i]
    if (!seen[v]) {
      seen[v] = true
      out.push(v)
    }
  }
  return out
}
console.time("unique_1"); unique_1(to_filter); console.timeEnd("unique_1")
// unique_1: 290.226ms (NodeJS)

let unique_2 = arr => {
  const seen = {}
  let out = []
  for (let v of arr) {
    if (!seen[v]) {
      seen[v] = true
      out.push(v)
    }
  }
  return out
}
console.time("unique_2"); unique_2(to_filter); console.timeEnd("unique_2")
// unique_2: 300.068ms (NodeJS) 

let unique_3 = arr => [...new Set(arr)]
console.time("unique_3"); unique_3(to_filter); console.timeEnd("unique_3")
// unique_3: 1.813s (NodeJS)

答案 69 :(得分:1)

要删除重复项,可能有两种情况。 首先,所有的数据都不是对象,其次,所有的数据都是对象。

如果所有数据都是任何一种原始数据类型,如 int、float、string 等,那么你可以遵循这个

const uniqueArray = [...new Set(oldArray)]

但是假设你的数组包含像波纹管这样的 JS 对象

{
    id: 1,
    name: 'rony',
    email: 'rony@example.com'
}

然后要获得所有独特的对象,您可以按照此操作

let uniqueIds = [];
const uniqueUsers = oldArray.filter(item => {
    if(uniqueIds.includes(item.id)){
        return false;
    }else{
        uniqueIds.push(item.id);
        return true;
    }
})

您也可以使用此方法使任何类型的数组成为唯一的。只需将跟踪键保留在 uniqueIds 数组上。

答案 70 :(得分:1)

2021:Node.js v12.14 和 v14.15 的性能测试

我从答案的首页获取了主要算法,并在 Node.js v12 中运行了一些性能数据,这是我的目标平台。 (Node.js v14.15 中的结果相似)。

我的代码:

var uniq1 = function(ar) {
  var item, j, len1, retar;
  retar = [];
  for (j = 0, len1 = ar.length; j < len1; j++) {
    item = ar[j];
    if (retar.indexOf(item) === -1 && item !== '') {
      retar.push(item);
    }
  }
  return retar;
};

var uniq2 = function(ar) {
  return [...new Set(ar)];
};

var uniq3 = function(ar, b, c) {
  b = ar.length;
  while (c = --b) {
    while (c--) {
      ar[b] !== ar[c] || ar.splice(c, 1);
    }
  }
  return ar;
};

var uniq4 = function(ar) {
  var onlyUnique;
  onlyUnique = function(value, index, self) {
    return self.indexOf(value) === index;
  };
  return ar.filter(onlyUnique);
};

var uniq5 = function(ar) {
  // lodash 4.5 uniq function
  return _.uniq(ar);
};

我调用此函数的应用程序有大约 50 个值,其中大约有 15 个唯一值。我生成了一些适合这种情况的随机数:

ar = [10,9,9,4,9,4,10,13,9,12,4,1,4,0,7,8,13,12,5,14,8,0,14,14,2,4,13,4,9,10,2,13,1,4,11,1,14,11,2,1,2,1,4,4,11,2,11,13,8,8]

然后我在 MacBook Pro c.2015 上运行每个测试用例 40,000 次,整个测试套件运行多次。尽管每次运行之间经过的时间略有不同,但以下数字是典型的:

✔ uniq1: 39 ms
✔ uniq2: 51 ms
✔ uniq3: 19 ms
✔ uniq4: 67 ms
✔ (lodash) uniq5: 24 ms

Uniq3 显然是最受欢迎的。 lodash 结果与 uniq3 非常相似,我认为经过的时间稍慢是因为我将 lodash 函数包装在我的函数中。 Lodash 可能使用与 uniq3 相同的算法。

有关每种算法的更多详细信息,请参阅此问题的答案首页。

答案 71 :(得分:0)

我查看了Joeytje50在jsperf上的代码,该代码比较了许多替代方案。他的代码有很多小错字,这对性能和正确性产生了影响。

更重要的是,他正在测试一个非常小的阵列。我创建了一个1000个整数的数组。每个整数是0到1000之间的随机整数的100倍。这使得平均约1000 / e = 368个重复。结果位于jsperf

这是一个更加现实的场景,可能需要提高效率。这些变化使声明发生了巨大的变化(具体而言,被称为最快的代码远远不够快)。明显的赢家是使用散列技术的地方,其中最好的是

Array.prototype.getUnique3 = function(){
   var u = Object.create(null), a = [];
   for(var i = 0, l = this.length; i < l; ++i){
      if(this[i] in u) continue;
      a.push(this[i]);
      u[this[i]] = 1;
   }
   return a.length;
}

答案 72 :(得分:0)

此脚本修改数组,过滤掉重复的值。它适用于数字和字符串。

https://jsfiddle.net/qsdL6y5j/1/

    Array.prototype.getUnique = function () {
        var unique = this.filter(function (elem, pos) {
            return this.indexOf(elem) == pos;
        }.bind(this));
        this.length = 0;
        this.splice(0, 0, unique);
    }

    var duplicates = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
    duplicates.getUnique();
    alert(duplicates);

此版本允许您返回一个具有唯一值的新数组,保留原始数据(只传递true)。

https://jsfiddle.net/dj7qxyL7/

    Array.prototype.getUnique = function (createArray) {
        createArray = createArray === true ? true : false;
        var temp = JSON.stringify(this);
        temp = JSON.parse(temp);
        if (createArray) {
            var unique = temp.filter(function (elem, pos) {
                return temp.indexOf(elem) == pos;
            }.bind(this));
            return unique;
        }
        else {
            var unique = this.filter(function (elem, pos) {
                return this.indexOf(elem) == pos;
            }.bind(this));
            this.length = 0;
            this.splice(0, 0, unique);
        }
    }

    var duplicates = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
    console.log('++++ ovveride')
    duplicates.getUnique();
    console.log(duplicates);
    console.log('++++ new array')
    var duplicates2 = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
    var unique = duplicates2.getUnique(true);
    console.log(unique);
    console.log('++++ original')
    console.log(duplicates2);

Browser support:

Feature Chrome  Firefox (Gecko)     Internet Explorer   Opera   Safari
Basic support   (Yes)   1.5 (1.8)   9                   (Yes)   (Yes)

答案 73 :(得分:0)

var a = [1,4,2,7,1,5,9,2,4,7,2]
var b = {}, c = {};
var len = a.length;
for(var i=0;i<len;i++){
  a[i] in c ? delete b[a[i]] : b[a[i]] = true;
  c[a[i]] = true;
} 

// b contains all unique elements

答案 74 :(得分:0)

使用字段[2]作为Id:

创建一个唯一数组的数组

&#13;
&#13;
const arr = [
  ['497', 'Q0', 'WTX091-B06-138', '0', '1.000000000', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B09-92', '1', '0.866899288', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B09-92', '2', '0.846036819', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B09-57', '3', '0.835025326', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B43-79', '4', '0.765068215', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B43-56', '5', '0.764211464', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B44-448', '6', '0.761701704', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B44-12', '7', '0.761701704', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B49-128', '8', '0.747434800', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B18-17', '9', '0.746724770', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B19-374', '10', '0.733379549', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B19-344', '11', '0.731421782', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B09-92', '12', '0.726450470', 'GROUP001'],
  ['497', 'Q0', 'WTX091-B19-174', '13', '0.712757036', 'GROUP001']
];


arr.filter((val1, idx1, arr) => !!~val1.indexOf(val1[2]) &&
  !(arr.filter((val2, idx2) => !!~val2.indexOf(val1[2]) &&
    idx2 < idx1).length));

console.log(arr);
&#13;
&#13;
&#13;

答案 75 :(得分:0)

在我使用对象的用例中,上面的对象答案似乎对我不起作用。

我对其进行了如下修改:

var j = {};

this.forEach( function(v) {
   var typ = typeof v;
   var v = (typ === 'object') ? JSON.stringify(v) : v;

   j[v + '::' + typ] = v;
});

return Object.keys(j).map(function(v){
  if ( v.indexOf('::object') > -1 ) {
    return JSON.parse(j[v]);
  }

  return j[v];
});

这似乎现在可以正确地用于对象,数组,具有混合值的数组,布尔值等。

答案 76 :(得分:0)

要过滤掉未定义和空值,因为大多数时候您不需要它们。

Severity Code Description Project File Line Suppression State Error TS6053 File 'C:/Users/.../my_project/node_modules/xlsx/types' not found. my_project JavaScript Content Files 1 Active

const uniques = myArray.filter(e => e).filter((e, i, a) => a.indexOf(e) === i);

答案 77 :(得分:0)

有时我需要从一组对象中获得唯一的出现。 Lodash似乎是一个很好的帮手,但我认为过滤数组并不能为项目添加依赖项。

让我们假设两个对象的比较是在比较属性(例如id)时构成的。

const a = [{id: 3}, {id: 4}, {id: 3}, {id: 5}, {id: 5}, {id: 5}];

由于我们都喜欢一行代码段,因此可以这样做:

a.reduce((acc, curr) => acc.find(e => e.id === curr.id) ? acc : [...acc, curr], [])

答案 78 :(得分:0)

该解决方案应该非常快,并且在许多情况下都可以使用。

  1. 将索引数组项转换为对象键
  2. 使用Object.keys函数

    var indexArray = ["hi","welcome","welcome",1,-9];
    var keyArray = {};
    indexArray.forEach(function(item){ keyArray[item]=null; });
    var uniqueArray = Object.keys(keyArray);
    

答案 79 :(得分:0)

我有一个简单的示例,我们可以从对象中具有重复ID的数组中删除对象,

  let data = new Array({id: 1},{id: 2},{id: 3},{id: 1},{id: 3});
  let unique = [];
  let tempArr = [];
  console.log('before', data);
  data.forEach((value, index) => {
    if (unique.indexOf(value.id) === -1) {
      unique.push(value.id);
    } else {
      tempArr.push(index);    
    }
  });
  tempArr.reverse();
  tempArr.forEach(ele => {
    data.splice(ele, 1);
  });
  console.log(data);

答案 80 :(得分:0)

现在使用集合,您可以删除重复项并将其转换回数组。

var names = ["Mike","Matt","Nancy", "Matt","Adam","Jenny","Nancy","Carl"];

console.log([...new Set(names)])

答案 81 :(得分:0)

很多人已经提到使用...

[...new Set(arr)];

这是一个很好的解决方案,但我更喜欢使用.filter的解决方案。我认为过滤器是获取唯一值的更自然的方法。您实际上是在删除重复项,而从数组中删除元素正是过滤器的目的。它还使您可以链接.map.reduce和其他.filter呼叫。我设计了这个解决方案...

const unique = () => {
  let cache;  
  return (elem, index, array) => {
    if (!cache) cache = new Set(array);
    return cache.delete(elem);
  };
};

myArray.filter(unique());

警告是您需要关闭,但我认为这是一个值得权衡的问题。在性能方面,它比我看到的其他使用.filter发布的解决方案性能更高,但性能却比[...new Set(arr)]差。

另请参阅我的github软件包youneek

答案 82 :(得分:0)

[...new Set(duplicates)]
  

这是最简单的方法,引用自 MDN Web Docs

const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
console.log([...new Set(numbers)]) // [2, 3, 4, 5, 6, 7, 32]

答案 83 :(得分:0)

您可以尝试以下方法:

function removeDuplicates(arr){
  var temp = arr.sort();
  for(i = 0; i < temp.length; i++){
    if(temp[i] == temp[i + 1]){
      temp.splice(i,1);
      i--;
    }
  }
  return temp;
}

答案 84 :(得分:0)

如果您只想获取唯一的元素并删除重复一次的元素,则可以执行以下操作:

let array = [2, 3, 4, 1, 2, 8, 1, 1, 2, 9, 3, 5, 3, 4, 8, 4];
function removeDuplicates(inputArray) {
  let output = [];
  for (value of array) {
    countObject[value] = (countObject[value] || 0) + 1;
  }
  for (key in countObject) {
    if (countObject[key] === 1) {
      output.push(key);
    }
  }
  return output;
}

console.log(removeDuplicates(array));

答案 85 :(得分:0)

   var elems = ['f', 'a','b','f', 'c','d','e','f','c', 'n', 'n'];

    elems.sort();

    elems.forEach(function (value, index, arr){

        let first_index = arr.indexOf(value);
        let last_index = arr.lastIndexOf(value);

         if(first_index === last_index){

         console.log('unique items in array ' + value);

         }else{

         console.log('Duplicate item in array ' + value);             

         }

    });

答案 86 :(得分:0)

您根本不需要.indexOf();您可以这样做O(n):

function SelectDistinct(array) {
    var seenIt = {};

    return array.filter(function (val) {
        if (seenIt[val]) 
            return false;
        return seenIt[val] = true;
    });
}

var hasDuplicates = [1,2,3,4,5,5,6,7,7];
console.log(SelectDistinct(hasDuplicates)) //[1,2,3,4,5,6,7]

如果您不想使用.filter():

function SelectDistinct(array) {
    var seenIt = {};
    var distinct = [];

    for (var i in array) {
        if (!seenIt[array[i]]) {
            seenIt[array[i]] = true;
            distinct.push(array[i]);
        }
    }

    return distinct;
}

答案 87 :(得分:0)

使用过滤器和接头

myArray.filter((el,i)=>![...myArray].splice(0,i).includes(el))

将Set与原型一起使用

Array.from(new Set(myArray))

将Set与点差运算符一起使用

[...new Set(myArray)]

答案 88 :(得分:0)

一种简单的解决方案,如果您想使用es6在无限数量的参数中检查唯一值。

function uniteUnique(arr, ...rest) {
      const newArr = arr.concat(rest).flat()
      return [...new Set(newArr)]
   }

console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]))
console.log(uniteUnique([1, 2, 3], [5, 2, 1]))

我希望它对某人有用。

答案 89 :(得分:0)

代码:

function RemoveDuplicates(array) {
    return array.filter(function (value, index, self) {
        return self.indexOf(value) === index;
    });
}

用法:

var arr = ["a","a","b","c","d", "d"];

console.log(RemoveDuplicates(arr));

结果:

0: "a"
1: "b"
2: "c"
3: "d"

答案 90 :(得分:0)

最简单的方法是将值转换为字符串以过滤嵌套对象的值。

install.packages("digest")

答案 91 :(得分:0)

array.forEach用于遍历数组中的每个元素,并通过if条件检查新数组是否已包含该元素。 希望这会有所帮助!

    var arr = [1, 2, 2, 3, 4, 5, 6, 7, 7, 7, 7, 8, 9, 4, 3, 3, 5];
    let arr1 = [];
    arr.forEach(elem => {
        if (!arr1.includes(elem)) {
            arr1.push(elem);
        }
    });
    console.log(arr1);

答案 92 :(得分:0)

在我的解决方案中,我在过滤之前先对数据进行排序:

const uniqSortedArray = dataArray.sort().filter((v, idx, t) => idx==0 || v != t[idx-1]); 

答案 93 :(得分:0)

这是使用reduce map的方法之一

const arr = [{id: '1'},{id: '4'},{id: '2'},{id: '1'},{id: '3'},{id: '1'},{id: '1'},{id: '5'}]

let uniqueArr = arr.reduce((arr, item) => {
  const uniq = arr.filter(i => i.id !== item.id)
    return [...uniq, item]
  }, [])

答案 94 :(得分:0)

您可以简单地使用内置函数Array.prototype.filter()

array.filter((x, y) => array.indexOf(x) == y);

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

arr = arr.filter((x, y) => arr.indexOf(x) == y);

console.log(arr);

答案 95 :(得分:0)

在 ES6 中你不需要像这样将集合转换为数组

[...new Set(arr)]

Array.from(new Set(arr))

set in mdn 说:Set 对象允许您存储任何类型的唯一值,无论是原始值还是对象引用。

所以它是独一无二的收藏.. 就用它

var arr = [2,2,2,2,1,3,5]

var unique = new Set(arr)
 
   for (let item of unique.keys()) console.log(item)
 

答案 96 :(得分:0)

就我而言,这是最简单的解决方案

// A way to check if the arrays are equal
const a = ['A', 'B', 'C'].sort().toString()
const b = ['A', 'C', 'B'].sort().toString()

console.log(a === b); // true


// Test Case
const data = [
  { group: 'A', name: 'SD' },
  { group: 'B', name: 'FI' },
  { group: 'A', name: 'SD' },
  { group: 'B', name: 'CO' }
];

// Return a new Array without dublocates
function unique(data) {
  return data.reduce(function (accumulator, currentValue) {
    // Convert to string in order to check if they are the same value.
    const currentKeys = Object.keys(currentValue).sort().toString();
    const currentValues = Object.values(currentValue).sort().toString();

    let hasObject = false
    
    for (const obj of accumulator) {
      // Convert keys and values into strings so we can
      // see if they are equal with the currentValue
      const keys = Object.keys(obj).sort().toString();
      const values = Object.values(obj).sort().toString();
      // Check if keys and values are equal
      if (keys === currentKeys && values === currentValues) {
        hasObject = true
      }
    }

    // Push the object if it does not exist already.
    if (!hasObject) {
      accumulator.push(currentValue)
    }

    return accumulator
  }, []);
}

// Run Test Case
console.log(unique(data)); // [ { group: 'A', name: 'SD' }, { group: 'B', name: 'FI' }, { group: 'B', name: 'CO' } ]

答案 97 :(得分:-1)

请不要引用我,但我认为您需要为您的属性名称使用字符串,例如o[e.toString()],然后在推送时将其转换回来。

答案 98 :(得分:-1)

如果顺序不重要,那么我们可以创建一个哈希并获取密钥以创建唯一数组。

var ar = [1,3,4,5,5,6,5,6,2,1];
var uarEle = {};
links.forEach(function(a){ uarEle[a] = 1; });
var uar = keys(uarEle)

uar将拥有独特的数组元素。

答案 99 :(得分:-1)

另一个答案,只是因为我为我的具体用例写了一个。我碰巧正在对数组进行排序,并且鉴于我正在排序,我可以使用它进行重复数据删除。

请注意,我的排序涉及我的特定数据类型,您可能需要根据您拥有的元素类型进行不同的排序。

var sortAndDedup = function(array) {
  array.sort(function(a,b){
    if(isNaN(a) && isNaN(b)) { return a > b ? 1 : (a < b ? -1 : 0); }
    if(isNaN(a)) { return 1; }
    if(isNaN(b)) { return -1; }
    return a-b;
  });

  var newArray = [];
  var len = array.length;
  for(var i=0; i<len; i++){
    if(i === 0 || array[i] != array[i-1]){
      newArray.push(array[i]);
    }
  }
};

答案 100 :(得分:-1)

 Array.prototype.unique=function(){ 

   var cr=[];

  this.forEach(function(entry) {
   if(cr.indexOf(entry)<0){
     cr.push(entry);
   }else{
    var index = cr.indexOf(entry);
    if (index > -1) {
     cr.splice(index, 1);
      }
   } 

 });

 return cr;
}

答案 101 :(得分:-1)

如果您想要更改它(不是创建新数组),您可以:

var
  uniq = function uniq(array) {
    var
      len = array.length;

    while (len--) {
      if (array.indexOf(array[len]) !== len) {
        array.splice(len, 1);
      }
    }

    return array;
  },

  myArray = [1, 2, 2, 4, 2];

console.log(uniq(myArray));
// [1, 2, 4];

答案 102 :(得分:-1)

对于字符串数组:

max-width

答案 103 :(得分:-1)

(function() {
    "use strict";

    Array.prototype.unique = function unique() {
        var self = this;
        return self.filter(function(a) {
            var that = this;
            // console.log(that);
            return !that[a] ? that[a] = true : false;
        }, {});
    }

    var sampleArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var distinctArray = sampleArray.unique();
    console.log(distinctArray);
})();
Here is the simple way to solve this problem...

答案 104 :(得分:-1)

我使用Array#reduce作为创建Array#unique

的方法

Array.prototype.unique = function() {
  var object = this.reduce(function(h, v) {
    h[v] = true;
    return h;
  }, {});
  return Object.keys(object);
}

console.log(["a", "b", "c", "b", "c", "a", "b"].unique()); // => ["a", "b", "c"]

答案 105 :(得分:-1)

你可以使用数组&#39;辅助函数reduce()和some()来实现您的结果。检查我的代码段:

&#13;
&#13;
var arrayWithDuplicates = [0, 0, 1, 2, 3, 3, 4, 4, 'a', 'a', '', '', null, null];

var arrayWithUniqueValues = arrayWithDuplicates
                            .reduce((previous, item) => {
                                if(!previous.some(element => element === item)) {
                                    previous.push(item)
                                }
                                return previous;
                            }, []);

console.log('arrayWithUniqueValues', arrayWithUniqueValues)
&#13;
&#13;
&#13;

答案 106 :(得分:-1)

在不使用O(n)的任何函数和时间复杂度的情况下使用它

function uni(arr){
   var i = 0,uk = 0;
    for (j = 1; j < arr.length; j++){
        if(arr[i] != arr[j]){
            uk++;
            i = j;
        }

    }
    return uk + 1;
}

答案 107 :(得分:-1)

使用 Array.prototype.includes() 方法删除重复项:

(function() {
    const array = [1, 1, 2, 2, 3, 5, 5, 2];
    let uniqueValues = [];
    array.map(num => {
        if (Number.isInteger(num) && !uniqueValues.includes(num)) {
            uniqueValues.push(num)
        }
    });
    console.log(uniqueValues)
}());