I have an array that looks like this
[
{
topic: 'Topic 1',
person: { ... },
unit: 'ABC-DEF'
},
{
topic: 'Topic 1',
person: { ... },
unit: 'ABC'
},
{
topic: 'Topic 1',
person: { ... },
unit: 'ABC-123-DEF'
},
{
topic: 'Topic 2',
person: { ... },
unit: 'ABC-123'
}
]
"units" are organisational units in my company. If there are duplicates by topic I want to keep only the object with the shortest unit and remove all others. So the example from above becomes:
[
{
topic: 'Topic 1',
person: { ... },
unit: 'ABC'
},
{
topic: 'Topic 2',
person: { ... },
unit: 'ABC-123'
}
]
I already took a look at uniqBy from lodash but how can I make sure that only the duplicate with the shortest unit stays in the array?