2013-05-03 259 views
4

我從我的Web服務中獲得這種列表。我想,以消除重複的用戶名:如何從列表中刪除重複

Mylist = [{ 
    "username": "Plr1", 
    "is_online": true, 
    "email": null, 
    "message": null, 
    "direction": 1, 
    "image_url": "" 
}, { 
    "username": "plr2", 
    "is_online": false, 
    "email": "", 
    "message": null, 
    "direction": 1, 
    "image_url": "" 
}, { 
    "username": "plr1", 
    "is_online": false, 
    "email": "", 
    "message": null, 
    "direction": 1, 
    "image_url": null 
}]; 

是否有一個功能,可以讓我刪除重複值(要素之一= Plr1)?

+0

使用一個循環來創建一個新的陣列或接頭(http://www.w3schools.com/jsref/jsref_splice.asp)項 – x4rf41 2013-05-03 10:59:47

+0

此鏈接可以對你有所幫助: http://stackoverflow.com/questions/8786197/remove-duplicates-from-an-array-which-contains-array – 2013-05-03 11:00:27

+0

['_.uniq(Mylist,function(x){return x.username})']( http://underscorejs.org/) – georg 2013-05-03 12:34:24

回答

1

DEMO:http://jsfiddle.net/abc123/ZgYbB/

注:最後一個for循環僅僅是表明項目均取剩下在數組中。

JS:

var Mylist= [{"username":"Plr1","is_online":true,"email":null,"message":null,"direction":1,"image_url":""},{"username":"plr2","is_online":false,"email":"","message":null,"direction":1,"image_url":""},{"username":"plr1","is_online":false,"email":"","message":null,"direction":1,"image_url":null} ]; 

for (var i = 0; i < Mylist.length; i++) { 
    for(var j = i + 1; j < Mylist.length; j++) { 
     if(Mylist[i].username.toLowerCase() == Mylist[j].username.toLowerCase()) 
      Mylist.splice(j, 1); 
    } 
} 

//Not needed just proof that items were removed. 
for (var i = 0; i < Mylist.length; i++) { 
    alert(Mylist[i].username); 
} 
+0

This是我早期答案的副本,它不會表現得很好(由於嵌套循環)......爲什麼給它打上勾號? – 2013-05-03 15:54:42

+0

我沒有複製你的答案,我jsfiddled並提出了我的答案。偉大的思想家們都這麼認爲,我同意有更快的方法。我的回答是一個簡單的例子,鑑於這個數組的大小,沒有任何問題會因操作時間而被注意到。 – abc123 2013-05-06 17:54:37

1

這將做你想做的事(假設你正在尋找不區分大小寫的用戶名比較)。

編輯補充演示:http://jsfiddle.net/vBZhR/

var Mylist= [{"username":"Plr1","is_online":true,"email":null,"message":null,"direction":1,"image_url":""},{"username":"plr2","is_online":false,"email":"","message":null,"direction":1,"image_url":""},{"username":"plr1","is_online":false,"email":"","message":null,"direction":1,"image_url":null} ]; 

dedupe(Mylist); 
function dedupe(list) { 
    var usernames = {}; 
    for (var i in list) { 
     if (usernames[list[i].username.toLowerCase()]) { 
      list.splice(i, 1); 
     } else { 
      usernames[list[i].username.toLowerCase()] = true; 
     } 
    } 
    return list; 
} 
+0

愛情在哪裏?這是第一個,正確的和最好的答案。爲什麼贊成後來的答案,表現不佳或被廣泛支持? – 2013-05-03 11:17:54

+0

你有證據來驗證你的要求?如果是這樣的話,演示我會在1秒內收到回覆,如果是這樣的話。 – abc123 2013-05-03 11:25:59

+0

演示添加:http://jsfiddle.net/vBZhR/ – 2013-05-03 15:53:53

3

您可以使用Array.filter

var Mylist = [{"username":"Plr1","is_online":true,"email":null,"message":null,"direction":1,"image_url":""},{"username":"plr2","is_online":false,"email":"","message":null,"direction":1,"image_url":""},{"username":"plr1","is_online":false,"email":"","message":null,"direction":1,"image_url":null} ]; 
var keys = []; 
var newList = Mylist.filter(
    function(x){ 
     var val = x.username.toLowerCase(); 
     if (!keys[val]) { 
      keys[val] = true; 
      return true; 
     } 
     return false; 
    } 
); 
keys = null; 
console.log(newList); 

它不會在舊的瀏覽器開箱的。如果你看看我發佈到上面的鏈接,有一個pollyfill讓他們工作。

+0

用於展示我喜歡的新方法的道具,展示新瀏覽器功能的+1 ...只要我們可以擺脫IE的世界<10 – abc123 2013-05-03 11:09:14