Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: Remove elements with a duplicate key-value pair from an array of unique objects except first such one

Note: This is not a duplicate question, as the elements are all unique. If the elements were the same I wouldn’t have asked. I am talking about a single key values…

I’m trying to find an efficient way to remove elements with a duplicate key-value pair from an array of unique objects except first such one. i.e.

Let’s say I have an array of object like below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    const array = [
        { checkID: 'aaa', animal: 'cat'},
        { checkID: 'bbb', animal: 'dog'},
        { checkID: 'ccc', animal: 'monkey'},
        { checkID: 'ddd', animal: 'horse'},
        { checkID: 'eee', animal: 'cow'},
        { checkID: 'fff', animal: 'cat'}
    ]

Here is the logic:

  1. The target key is ‘animal’
  2. If there is no other element with the same value for the field, keep it.
  3. If there are duplicate elements with the same value for the field but it is the first one, keep it.
  4. If there are duplicate elements with the same value for the field and it is not the first one, delete it.

As a result, the array should be:

    const result = [
        { checkID: 'aaa', animal: 'cat'},
        { checkID: 'bbb', animal: 'dog'},
        { checkID: 'ccc', animal: 'monkey'},
        { checkID: 'ddd', animal: 'horse'},
        { checkID: 'eee', animal: 'cow'}
    ]

The elements are unique and it makes the job complicated. Tried creating a separate array with the ‘animal’ values and then using the filter method but the code got messy and thought there might be a better way.

Any suggestions? Thanks

>Solution :

You could take a Set in a closure with the wanted key for filtering.

const
    uniqueBy = (k, s = new Set) => o => !s.has(o[k]) && s.add(o[k]),
    array = [{ checkID: 'aaa', animal: 'cat' }, { checkID: 'bbb', animal: 'dog' }, { checkID: 'ccc', animal: 'monkey' }, { checkID: 'ddd', animal: 'horse' }, { checkID: 'eee', animal: 'cow' }, { checkID: 'fff', animal: 'cat' }],
    result = array.filter(uniqueBy('animal'));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading