获取JavaScript数组中的所有唯一值(删除重复项) [英] Get all unique values in a JavaScript array (remove duplicates)
问题描述
我有一系列数字,我需要确保它们是唯一的。我在互联网上找到了下面的代码片段,它的工作情况很好,直到数组中的数字为零。我发现这里的这个其他脚本看起来几乎与它完全一样,但它不会失败。
I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on SO that looks almost exactly like it, but it doesn't fail.
所以为了帮助我学习,有人可以帮我确定原型脚本出错的地方吗?
So for the sake of helping me learn, can someone help me determine where the prototype script is going wrong?
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;
}
来自重复提问的更多答案:
- 从JavaScript阵列中删除重复项
- Remove Duplicates from JavaScript Array
- Get all values with more than one occurrence (i.e.: not unique) in an array
More answers from duplicate question:
推荐答案
使用 JavaScript 1.6 / ECMAScript 5 ,您可以使用原生 filter
方法中的数组以下方式获取具有唯一值的数组:
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:
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
。
The native method filter
will loop through the array and leave only those entries that pass the given callback function onlyUnique
.
onlyUnique
检查,如果给定的值是第一次发生的话。如果没有,它必须是重复的,不会被复制。
onlyUnique
checks, if the given value is the first occurring. If not, it must be a duplicate and will not be copied.
此解决方案无需任何额外的库,如jQuery或prototype.js。
This solution works without any extra library like jQuery or prototype.js.
它适用于具有混合值类型的数组。
It works for arrays with mixed value types too.
对于旧浏览器(< ie9),不支持本机方法 filter
和 indexOf
你可以在MDN文档中找到 filter 和 indexOf 。
For old Browsers (<ie9), that do not support the native methods filter
and indexOf
you can find work arounds in the MDN documentation for filter and indexOf.
如果你想保留最后一次出现一个值,简单地用 lastIndexOf
替换 indexOf
。
If you want to keep the last occurrence of a value, simple replace indexOf
by lastIndexOf
.
使用ES6可以缩短到这一点:
With ES6 it could be shorten to this:
// 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']
感谢 Camilo Martin 提供的评论提示。
Thanks to Camilo Martin for hint in comment.
ES6有一个原生对象 设置
以存储唯一值。要获得具有唯一值的数组,您现在可以这样做:
ES6 has a native object Set
to store unique values. To get an array with unique values you could do now this:
var myArray = ['a', 1, 'a', 2, '1'];
let unique = [...new Set(myArray)];
// unique is ['a', 1, 2, '1']
Set
的构造函数采用可迭代对象(如Array)和扩展运算符 ...
转换设置回一个数组。感谢 Lukas Liese 提供的评论提示。
The constructor of Set
takes an iterable object, like Array, and the spread operator ...
transform the set back into an Array. Thanks to Lukas Liese for hint in comment.
这篇关于获取JavaScript数组中的所有唯一值(删除重复项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!