獲取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
相關文章
相關標籤/搜索