js中的对象数组:删除重复项

时间:2016-01-02 13:55:45

标签: javascript arrays

我有一个包含许多对象的数组:

[{"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]);

我只想在firstName,lastName和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];,但这不起作用。

有谁可以帮我解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以通过组合3个值

来形成唯一键
$new = ['*', '*', '*'];

像这样:

firstName + lastName + linkedInID