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');
5
  • 1
    Maybe look at the answers from this earlier question Commented Jun 29, 2022 at 8:09
  • @Andy This looks like a work of a bot or something. Should we flag it for moderator? Commented Jun 29, 2022 at 8:13
  • 1
    It's probably just a homework question from two different students. It happens quite regularly. Commented Jun 29, 2022 at 8:14
  • @Andy what's the SO policy regarding this? Should we close it? Commented Jun 29, 2022 at 8:17
  • I've gone "needs more focus" as a shortcut. No need to bother the mods at this point. Commented Jun 29, 2022 at 8:21

3 Answers 3

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}, 
{}))
Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.