0

I want to delete duplicate in array of objects but not as I found in stackoverflow questions,I want to check more than one property and verify that is not duplicate,for example :

 const input =  [
        {
          "eventUid": "0fdb73d9-629f-4151-acab-7b48c24ef2D0",
          "name": "John",
          "lastName": "Doe",
          "city": "Ukraine",
         
        },
        {
          "eventUid": "0fdb73d9-629f-4151-aBab-7b48c24ef2e0",
          "name": "Marcel",
          "lastName": "Pilate",
          "city": "Ukraine",
        },
        {
          "eventUid": "0fcc73d9-629f-4151-aBab-7b48c24ef2e0",
          "name": "John",
          "lastName": "Doe",
          "city": "Ukraine",
        }
      ]

Output shoukd be :

const input =  [
        {
          "eventUid": "0fdb73d9-629f-4151-acab-7b48c24ef2D0",
          "name": "John",
          "lastName": "Doe",
          "city": "Ukraine",
         
        },
        {
          "eventUid": "0fdb73d9-629f-4151-aBab-7b48c24ef2e0",
          "name": "Marcel",
          "lastName": "Pilate",
          "city": "Ukraine",
        }
      ]

Because I want to check name,lastName and city for each element of the array, if there are duplicate then keep only the first. Have you any idea how to check that ?

8
  • Not work for objects.. Commented Aug 17, 2023 at 12:09
  • Are you saying eventUid is not the identifier? Commented Aug 17, 2023 at 12:13
  • The answer with the most upvotes in the linked question is just terrible Commented Aug 17, 2023 at 12:15
  • 1
    @ChristianVincenzoTraina but this one is elegant Commented Aug 17, 2023 at 12:33
  • @alainber82 so the first found will be kept? Commented Aug 17, 2023 at 12:33

1 Answer 1

2

You can combine Array.prototype.filter and Array.prototype.findLastIndex.

The concept is that, while iterating the items, if the current index is equal to the last index then it exists twice, thus it can be removed.

Since you are comparing objects, and you want to check only 3 properties out of 4, you cannot use lastIndexOf. But instead you want to search the last index in the array with findLastIndex.

const filtered = 
  input.filter((item, index) => 
    input.findLastIndex(innerItem => 
        innerItem.name === item.name && 
        innerItem.lastName === item.lastName && 
        innerItem.city === item.city
    ) === index);

const input = [{
          "eventUid": "0fdb73d9-629f-4151-acab-7b48c24ef2D0",
          "name": "John",
          "lastName": "Doe",
          "city": "Ukraine",
         
        },
        {
          "eventUid": "0fdb73d9-629f-4151-aBab-7b48c24ef2e0",
          "name": "Marcel",
          "lastName": "Pilate",
          "city": "Ukraine",
        },
        {
          "eventUid": "0fcc73d9-629f-4151-aBab-7b48c24ef2e0",
          "name": "John",
          "lastName": "Doe",
          "city": "Ukraine",
        }
      ];
      
const filtered = input.filter((item, index) => input.findLastIndex(innerItem => innerItem.name === item.name && innerItem.lastName === item.lastName && innerItem.city === item.city) === index)

console.log(filtered);

Sign up to request clarification or add additional context in comments.

3 Comments

I think you meant to say that when the current index is NOT equal to the last index.
@0xRm Yes you're right, and when update with your idea it works!
It depends on if you want to keep the first or the last match. Usually it doesn't matter, but in this case it matters since they have different eventUids

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.