1

I have an array with a number of objects:

[{"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}, ... ]

It's possible that there are duplicates, and I wish to remove them.

This answer got my to the point were I can remove duplicates based on the last name:

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]);

I only want a person to be removed when firstName, lastName and linkedInID is the exact same.

I've tried changing arr[attendees_in_one_array[i]['lastName']] = attendees_in_one_array[i]; to arr[attendees_in_one_array[i]['lastName'] && attendees_in_one_array[i]['firstName'] && attendees_in_one_array[i]['linkedInID']] = attendees_in_one_array[i]; but that didn't work.

Anyone who can help me with this?

Community
  • 1
  • 1
binoculars
  • 2,226
  • 5
  • 33
  • 61
  • Aren't the `linedInID`s unique? I would think they would be. If so, why not just use those? If not, your accepted answer can remove items that shouldn't be removed. –  Jan 02 '16 at 15:10
  • Not every user had a LinkedIn ID, so I can't remove on duplicate LinkedIn IDs. You say the accepted answer can remove items that should't be removed. Could you please give an example in which case that is? – binoculars Jan 02 '16 at 16:10
  • Take two users that do not have a LinkedInID. Let's say the first user is `firstName: "foo", lastName: "bar"` and the second user is `firstName: "fo", lastName: "obar"`. The `firstName + lastName` in both cases will be `foobar`, so the second one will be removed. –  Jan 02 '16 at 16:19

1 Answers1

1

You can form the unique key from the combination of the 3 values

firstName + lastName + linkedInID

Like this :

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 
loxxy
  • 12,990
  • 2
  • 25
  • 56