获取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上找到了另外一个脚本 ,看起来几乎彻底同样,可是它不会失败。 数组

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;
}

More answers from duplicate question: 来自重复问题的更多答案:

Similar question: 相似的问题:


解决方案:

参考一: https://stackoom.com/question/8E0X/获取JavaScript数组中的全部惟一值-删除重复项
参考二: https://oldbug.net/q/8E0X/Get-all-unique-values-in-a-JavaScript-array-remove-duplicates
相关文章
相关标签/搜索