I have the following data structure that i receive from the api:
[
{
cat_id: '10000844',
cat_id_full: '10000844-01',
order: '20',
},
{
cat_id: '10000844',
cat_id_full: '10000844-02',
order: '50',
},
{
cat_id: '50000844',
cat_id_full: '50000844-52',
order: '10',
},
{
cat_id: '80000844',
cat_id_full: '80000844-32',
order: '51',
},
{
cat_id: '80000844',
cat_id_full: '80000844-12',
order: '12',
},
]
The perfect outcome of cleaning the code would be this result, basically returning only the non-duplicated array sorted by the order:
[
{
cat_id: '10000844',
cat_id_full: '10000844-01',
order: '20',
},
{
cat_id: '50000844',
cat_id_full: '50000844-52',
order: '10',
},
{
cat_id: '80000844',
cat_id_full: '80000844-12',
order: '12',
},
]
But currently it only returns the first found duplicate in a unique array, with the current code (using lodash uniqBy, but it does not have to be one using lodash):
const uniqueCats = uniqBy(cats, 'cat_id');