Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
remove duplicates of array of objects by the lowest order javascript

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');
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

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}, 
{}))
about 4 years ago · Juan Pablo Isaza Relatório

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;
}
about 4 years ago · Juan Pablo Isaza Relatório

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)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda