获取 JavaScript 数组中的所有唯一值(删除重复项)
我有一组数字,需要确保它们是唯一的。我在互联网上找到了下面的代码片段,它工作得很好,直到数组中有零。我在 Stack Overflow 上发现了这个其他脚本,它看起来几乎与它一模一样,但它并没有失败。
那么为了帮助我学习,有人可以帮助我确定原型脚本出了问题吗?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
通过 JavaScript 1.6 / ECMAScript 5,您可以使用本机
filter
数组的方法,通过以下方式获取具有唯一值的数组:本机方法
filter
将循环遍历数组,并仅保留那些传递给定回调函数onlyUnique
的条目。onlyUnique
检查给定值是否是第一次出现。如果不是,则一定是重复的,不会被复制。该解决方案无需任何额外的库(如 jQuery 或prototype.js)即可工作。
它也适用于具有混合值类型的数组。
对于不支持本机方法过滤器 和 indexOf。
filter
和indexOf
的旧浏览器 (如果您想保留最后一次出现的值,只需将
indexOf
替换为lastIndexOf
即可。使用 ES6,这可以缩短为:
感谢 Camilo Martin 在评论中提供提示。
ES6 有一个原生对象
Set
存储唯一值。要获取具有唯一值的数组,您现在可以执行以下操作:Set
的构造函数采用一个可迭代对象,例如数组,而展开运算符...
将集合转换回数组。感谢 Lukas Liese 在评论中提供提示。With JavaScript 1.6 / ECMAScript 5 you can use the native
filter
method of an Array in the following way to get an array with unique values:The native method
filter
will loop through the array and leave only those entries that pass the given callback functiononlyUnique
.onlyUnique
checks, if the given value is the first occurring. If not, it must be a duplicate and will not be copied.This solution works without any extra library like jQuery or prototype.js.
It works for arrays with mixed value types too.
For old Browsers (<ie9), that do not support the native methods
filter
andindexOf
you can find work arounds in the MDN documentation for filter and indexOf.If you want to keep the last occurrence of a value, simply replace
indexOf
withlastIndexOf
.With ES6 this can be shorten to:
Thanks to Camilo Martin for hint in comment.
ES6 has a native object
Set
to store unique values. To get an array with unique values you could now do this:The constructor of
Set
takes an iterable object, like an Array, and the spread operator...
transform the set back into an Array. Thanks to Lukas Liese for hint in comment.更新了 ES6/ES2015 的答案:使用 设置和展开运算符 (感谢le-m),单行解决方案是
:
Updated answer for ES6/ES2015: Using the Set and the spread operator (thanks le-m), the single line solution is:
Which returns
我将所有答案分为 4 个可能的解决方案:
{ }
防止重复[ ]
filter + indexOf
设置
构造函数。以下是答案中找到的示例代码:
使用对象
{ }
防止重复使用辅助数组
[ ]
使用
filter + indexOf
使用 ES6< /strong>
[...new Set(a)]
我想知道哪个更快。我制作了示例 Google 表格来测试功能。注意:ECMA 6 在 Google Sheets 中不可用,因此我无法对其进行测试。
这是测试结果:
我希望看到使用对象
{ }
的代码会获胜,因为它使用哈希。所以我很高兴测试显示该算法在 Chrome 和 IE 中获得了最佳结果。感谢 @rab 提供的代码。更新 2020
Google 脚本启用了 ES6 引擎。现在我用
Set
测试了最后一个代码,它看起来比对象方法更快。I split all answers to 4 possible solutions:
{ }
to prevent duplicates[ ]
filter + indexOf
Set
constructor.Here's sample codes found in answers:
Use object
{ }
to prevent duplicatesUse helper array
[ ]
Use
filter + indexOf
Use ES6
[...new Set(a)]
And I wondered which one is faster. I've made sample Google Sheet to test functions. Note: ECMA 6 is not avaliable in Google Sheets, so I can't test it.
Here's the result of tests:
I expected to see that code using object
{ }
will win because it uses hash. So I'm glad that tests showed the best results for this algorithm in Chrome and IE. Thanks to @rab for the code.Update 2020
Google Script enabled ES6 Engine. Now I tested the last code with
Set
and it appeared faster than the object method.您还可以使用 underscore.js。
这将返回:
You can also use underscore.js.
which will return:
One Liner,纯 JavaScript
使用 ES6 语法
list = list.filter((x, i, a) => a.indexOf(x) == i)
采用 ES5 语法
浏览器兼容性:IE9+
One Liner, Pure JavaScript
With ES6 syntax
list = list.filter((x, i, a) => a.indexOf(x) == i)
With ES5 syntax
Browser Compatibility: IE9+
这里的许多答案可能对初学者没有用。如果对数组进行重复数据删除很困难,他们真的了解原型链,甚至了解 jQuery 吗?
在现代浏览器中,一个干净简单的解决方案是将数据存储在 Set,它被设计为唯一值的列表。
Array.from
对于将 Set 转换回数组非常有用,这样您就可以轻松访问数组具有的所有出色方法(功能)。还有其他方法可以完成同样的事情。但您可能根本不需要 Array.from,因为 Sets 有很多有用的功能,例如 forEach。如果您需要支持旧版 Internet Explorer,因此无法使用 Set,那么一种简单的技术是将项目复制到新数组,同时事先检查它们是否已在新数组中。
为了使其可以立即重用,我们将其放入一个函数中。
因此,为了消除重复项,我们现在就这样做。
当函数完成时,
deduplicate(cars)
部分变成我们命名的结果。只需将您喜欢的任何数组的名称传递给它即可。
Many of the answers here may not be useful to beginners. If de-duping an array is difficult, will they really know about the prototype chain, or even jQuery?
In modern browsers, a clean and simple solution is to store data in a Set, which is designed to be a list of unique values.
The
Array.from
is useful to convert the Set back to an Array so that you have easy access to all of the awesome methods (features) that arrays have. There are also other ways of doing the same thing. But you may not needArray.from
at all, as Sets have plenty of useful features like forEach.If you need to support old Internet Explorer, and thus cannot use Set, then a simple technique is to copy items over to a new array while checking beforehand if they are already in the new array.
To make this instantly reusable, let's put it in a function.
So to get rid of the duplicates, we would now do this.
The
deduplicate(cars)
part becomes the thing we named result when the function completes.Just pass it the name of any array you like.
此后我找到了一个使用 jQuery 的好方法
注意:此代码是从 Paul Irish 的鸭子中提取的打卡帖子 - 我忘了注明出处:P
I have since found a nice method that uses jQuery
Note: This code was pulled from Paul Irish's duck punching post - I forgot to give credit :P
神奇的
O(n) 性能 - 我们假设您的数组位于
a
和t={}
中。说明此处 (+Jeppe< /a> 展示次数。)Magic
O(n) performance - we assume your array is in
a
andt={}
. Explanation here (+Jeppe impr.)我们可以使用 ES6 集来做到这一点:
We can do this using ES6 sets:
最简单、最快(在 Chrome 中)的方法:
简单地遍历数组中的每个项目,进行测试如果该项目已在列表中,如果不在列表中,则推送到返回的数组。
根据 JSBench 的说法,这个函数是我能在任何地方找到的最快的函数 - 不过请随意添加您自己的函数。
非原型版本:
排序
当还需要对数组进行排序时,以下是最快的:
或非原型:
这也是 在大多数非 Chrome 浏览器中比上述方法更快。
The simplest, and fastest (in Chrome) way of doing this:
Simply goes through every item in the array, tests if that item is already in the list, and if it's not, pushes to the array that gets returned.
According to JSBench, this function is the fastest of the ones I could find anywhere - feel free to add your own though.
The non-prototype version:
Sorting
When also needing to sort the array, the following is the fastest:
or non-prototype:
This is also faster than the above method in most non-Chrome browsers.
原始值
带有
Set
(推荐)没有
设置
对象
此示例展示了如何不仅过滤原始值数组,还过滤对象数组。我添加了注释,以便您更轻松地了解可以根据您的要求进行更改的内容。
Primitive values
With
Set
(Recommended)Without
Set
Objects
This example shows how you can filter not just an array of primitive values but an array of objects. I have added comments to make it easier to understand what you can change there depending on your requirements.
这个问题已经得到了很多回答,但并没有满足我的特殊需求。
许多答案都是这样的:
但这不适用于复杂对象的数组。
假设我们有一个像这样的数组:
如果我们想要具有唯一名称的对象,我们应该使用 array.prototype.findIndex 而不是 array.prototype.indexOf:
This has been answered a lot, but it didn't address my particular need.
Many answers are like this:
But this doesn't work for arrays of complex objects.
Say we have an array like this:
If we want the objects with unique names, we should use
array.prototype.findIndex
instead ofarray.prototype.indexOf
:在查看了这里所有 90+ 答案后,我发现还有一个空间:
Array.includes 有一个非常方便的第二个参数:"fromIndex",因此通过使用它,每次迭代
filter
回调方法将从[当前索引] + 1
开始搜索数组,其中保证不包含< em>当前在查找中过滤了项目,并且还节省了时间。After looking into all the 90+ answers here, I saw there is room for one more:
Array.includes has a very handy second-parameter: "fromIndex", so by using it, every iteration of the
filter
callback method will search the array, starting from[current index] + 1
which guarantees not to include currently filtered item in the lookup and also saves time.Explanation:
For example, lets assume the
filter
function is currently iterating at index2
) and the value at that index happens to be2
. The section of the array that is then scanned for duplicates (includes
method) is everything after index 2 (i+1
):And since the currently filtered item's value
2
is included in the rest of the array, it will be filtered out, because of the leading exclamation mark which negates the filter rule.If order is important, use this method:
这个原型
getUnique
并不完全正确,因为如果我有一个像这样的数组:["1",1,2,3,4,1,"foo"]
它将返回["1","2","3","4"]
并且"1"
是字符串,1
是整数;他们是不同的。这是一个正确的解决方案:
使用:
上面将产生
["1",2,3,4,1,"foo"]
。This prototype
getUnique
is not totally correct, because if i have a Array like:["1",1,2,3,4,1,"foo"]
it will return["1","2","3","4"]
and"1"
is string and1
is a integer; they are different.Here is a correct solution:
using:
The above will produce
["1",2,3,4,1,"foo"]
.这是因为
0
在 JavaScript 中是一个假值。如果数组的值为 0 或任何其他虚假值,则
this[i]
将为虚假值。That's because
0
is a falsy value in JavaScript.this[i]
will be falsy if the value of the array is 0 or any other falsy value.无需扩展 Array.prototype (据说这是一种不好的做法)或使用 jquery/underscore,您可以简单地
过滤
数组。通过保留最后一次出现:
或第一次出现:
嗯,它只是 javascript ECMAScript 5+,这意味着只有 IE9+,但对于本机 HTML/JS 的开发(Windows Store App、Firefox OS、Sencha、Phonegap、Titanium,.. .)。
Without extending Array.prototype (it is said to be a bad practice) or using jquery/underscore, you can simply
filter
the array.By keeping last occurrence:
or first occurrence:
Well, it's only javascript ECMAScript 5+, which means only IE9+, but it's nice for a development in native HTML/JS (Windows Store App, Firefox OS, Sencha, Phonegap, Titanium, ...).
简单的。
Simple.
现在使用集合,您可以删除重复项并将它们转换回数组。
另一种解决方案是使用 sort &筛选
Now using sets you can remove duplicates and convert them back to the array.
Another solution is to use sort & filter
如果您使用 Prototype 框架,则无需执行“for”循环,您可以使用 http://prototypejs.org/doc/latest/language/Array/prototype/uniq/ 像这样:
这将产生一个没有重复项的重复数组。我在搜索一种计算不同数组记录的方法时遇到了您的问题,因此在
uniq()
之后我使用了size()
并且得到了简单的结果。ps 抱歉,如果我输入错误,
请编辑:如果您想转义未定义的记录,您可能需要在之前添加
compact()
,如下所示:If you're using Prototype framework there is no need to do 'for' loops, you can use http://prototypejs.org/doc/latest/language/Array/prototype/uniq/ like this:
Which will produce a duplicate array with no duplicates. I came across your question searching a method to count distinct array records so after
uniq()
I usedsize()
and there was my simple result.p.s. Sorry if i mistyped something
edit: if you want to escape undefined records you may want to add
compact()
before, like this:我遇到了一个稍微不同的问题,我需要从数组中删除具有重复 id 属性的对象。这有效。
I had a slightly different problem where I needed to remove objects with duplicate id properties from an array. this worked.
如果您可以接受额外的依赖项,或者您的代码库中已经有其中一个库,则可以使用 LoDash(或 Underscore)从数组中删除重复项。
用法
如果您的代码库中还没有它,请使用 npm 安装它:
然后按如下方式使用它:
输出:
If you're okay with extra dependencies, or you already have one of the libraries in your codebase, you can remove duplicates from an array in place using LoDash (or Underscore).
Usage
If you don't have it in your codebase already, install it using npm:
Then use it as follows:
Out:
在 ES6/更高版本中
仅获取唯一值
获取唯一对象
In ES6/Later
Get Only Unique Values
Get Unique Objects
我不知道为什么 Gabriel Silveira 以这种方式编写该函数,但对我来说同样有效且无需缩小的更简单的形式是:
或在 CoffeeScript 中:
I'm not sure why Gabriel Silveira wrote the function that way but a simpler form that works for me just as well and without the minification is:
or in CoffeeScript:
用简单的方法查找唯一的数组值
Finding unique Array values in simple method
看来我们已经失去了拉斐尔的答案,它多年来一直是公认的答案。这是(至少在 2017 年)性能最佳的解决方案如果您没有混合类型数组 :
如果您确实有混合类型数组,则可以序列化哈希键:
It appears we have lost Rafael's answer, which stood as the accepted answer for a few years. This was (at least in 2017) the best-performing solution if you don't have a mixed-type array:
If you do have a mixed-type array, you can serialize the hash key:
奇怪的是,以前没有建议过这一点。要通过数组中的对象键(下面的
id
)删除重复项,您可以执行以下操作:strange this hasn't been suggested before.. to remove duplicates by object key (
id
below) in an array you can do something like this:对于具有一些唯一 id 的基于对象的数组,我有一个简单的解决方案,您可以通过它按线性复杂度进行排序
For an object-based array with some unique id's, I have a simple solution through which you can sort in linear complexity