获取JavaScript数组中的所有唯一值(删除重复项) - Get all unique values in a JavaScript array (remove duplicates)

08/12 04:40
阅读数 54

问题:

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
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部