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

168
Visualizações
How can I remove duplicate objects and sort?

I'm fetching pokemon names and images, and storing them like this.

   const pokemon = $(el)
      .find(".pokemon")
      .toArray()
      .map((el) => {
        const name = $(el).attr("title") || "";
        const image = $(el).find("img").attr("src") || "";
        return { name, image };
      });

I want a seperate variable that sorts by most reoccurring and then removes duplicates. I tried using byCount() from this:

Array.prototype.byCount = function () {
  var itm,
    a = [],
    L = this.length,
    o = {};
  for (var i = 0; i < L; i++) {
    itm = this[i];
    if (!itm) continue;
    if (o[itm] == undefined) o[itm] = 1;
    else ++o[itm];
  }
  for (var p in o) a[a.length] = p;
  return a.sort(function (a, b) {
    return o[b] - o[a];
  });
};

and it worked if i fetch the name / image separately, but doesn't seem to work on an object. the data is like

[name1, name1, name2, name3, name3, name4]
[img1, img1, img2, img3, img3, img4]

if i sort them individually using byCount() they don't always align so it might return:

[name1, name3, name2, name4]
[img3, img1, img2, img4]

since both img1 & img3 have the same occurance count.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

const repeatedItems = {}
arrayOfObjects.forEach((item) => {
  if (repeatedItems[item.name]) {
    repeatedItems[item.name] = { ...item, count: repeatedItems[item.name].count++ }
  } else {
    repeatedItems[item.name] = { ...item, count: 1 }
  }
})
const sortedArr = Object.values(repeatedItems).sort((a, b) => a.count - b.count).map((item) => ({ name: item.name, image: item.image }))
about 4 years ago · Juan Pablo Isaza Relatório

0

To sort words with the prototype sort, use the localeCompare String prototype:


items.sort((a, b) => a.localeCompare(b))

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