0

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');
kaster
  • 1
  • 2
  • 1
    [Maybe look at the answers from this earlier question](https://stackoverflow.com/questions/72796788/get-the-first-of-duplicates-in-array-of-objects-javascript/72797239) – Andy Jun 29 '22 at 08:09
  • @Andy This looks like a work of a bot or something. Should we flag it for moderator? – holydragon Jun 29 '22 at 08:13
  • 1
    It's probably just a homework question from two different students. It happens quite regularly. – Andy Jun 29 '22 at 08:14
  • @Andy what's the SO policy regarding this? Should we close it? – Radu Diță Jun 29 '22 at 08:17
  • I've gone "needs more focus" as a shortcut. No need to bother the mods at this point. – Andy Jun 29 '22 at 08:21

3 Answers3

0

How about a quick reduce:

Object.values(a.reduce((acc, current) => { 
    if(!acc[current.cat_id]) {
        acc[current.cat_id] = current
    } else {
        if (acc[current.cat_id].order > current.order) {
            acc[current.cat_id] = current
        }
    }

    return acc}, 
{}))
Radu Diță
  • 13,476
  • 2
  • 30
  • 34
0

Try this

function removeDublicats(arr = [], proprety = "") {
  const set = new Set();
  const result = [];
  for (let i = 0; i < arr.length; i++) {
    const item = arr[i];
    if (set.has(item[proprety])) continue;
    set.add(item[proprety]);
    result.push(item);
  }
  return result;
}
Ahmed Abdalla
  • 102
  • 1
  • 4
0

You can use Array.prototype.reduce() and extract array result with Object.values()

Code:

const cats = [{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',},]

const uniqBy = (arr, key) => Object.values(
  arr.reduce((a, c) => {
    a[c[key]] = a[c[key]]?.order < c.order
      ? a[c[key]]
      : c
    return a
  }, {}))

const uniqueCats = uniqBy(cats, 'cat_id')

console.log(uniqueCats)
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46