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?

3
  • Aren't the linedInIDs 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. Commented Jan 2, 2016 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? Commented Jan 2, 2016 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. Commented Jan 2, 2016 at 16:19

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.