2016-01-02 86 views
1

我有一些對象的數組:數組中的JS對象:刪除重複

[{"firstName":"an","lastName":"something","linkedInID":"..."},{"firstName":"stefanie","lastName":"doe","linkedInID":"..."},{"firstName":"john","lastName":"something","linkedInID":null},{"firstName":"timmy","lastName":"test","linkedInID":null},{"firstName":"john","lastName":"something","linkedInID":null}, ... ] 

有重複,我想刪除這是可能的。

This回答了我的一點是我可以根據姓氏刪除重複項:

var arr = {}; 

for (var i=0, len=attendees_in_one_array.length; i < len; i++) 
    arr[attendees_in_one_array[i]['lastName']] = attendees_in_one_array[i]; 

attendees_in_one_array = new Array(); 
for (var key in arr) 
    attendees_in_one_array.push(arr[key]); 

我只想當名字,姓氏和linkedInID是完全相同的被刪除的人。

我試過將arr[attendees_in_one_array[i]['lastName']] = attendees_in_one_array[i];更改爲arr[attendees_in_one_array[i]['lastName'] && attendees_in_one_array[i]['firstName'] && attendees_in_one_array[i]['linkedInID']] = attendees_in_one_array[i];,但沒有奏效。

任何人都可以幫助我?

+0

不是'linedInID's獨特之處?我會認爲他們會。如果是這樣,爲什麼不使用這些?如果不是,您接受的答案可以刪除不應刪除的項目。 –

+0

並非每個用戶都有LinkedIn ID,所以我無法刪除重複的LinkedIn ID。你說接受的答案可以刪除不應刪除的項目。你能舉個例子嗎? – binoculars

+0

帶兩位沒有LinkedInID的用戶。假設第一個用戶是firstName:「foo」,lastName:「bar」,第二個用戶是firstName:「fo」,lastName:「obar」。在這兩種情況下,「firstName + lastName」將是'foobar',所以第二個將被刪除。 –

回答

1

可以形成從3個值

firstName + lastName + linkedInID 

像這樣的組合的唯一密鑰:

var arr = [{"firstName":"an","lastName":"something","linkedInID":"..."},{"firstName":"stefanie","lastName":"doe","linkedInID":"..."},{"firstName":"john","lastName":"something","linkedInID":null},{"firstName":"timmy","lastName":"test","linkedInID":null},{"firstName":"john","lastName":"something","linkedInID":null} ] 

var ulist = {}, uarr =[]; 
arr.forEach(function(k) { ulist[k.firstName + k.lastName + k.linkedInID] = k; }); 
for(var i in ulist) uarr.push(ulist[i]); 

// uarr contains your desired result