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

223
Visualizações
how to Search for duplicates in list (javascript)?

I try to look for duplicates in the array and get an error, and I glad for any solution for this problem Attached is code:

let names = itemList[0].getElementsByTagName("span")[0].innerText;
for (i = 1; i < itemList.length; i++) {
  if (!(itemList[i].getElementsByTagName("span")[0].innerText in names)) {
    names.push(itemList[i].getElementsByTagName("span")[0].innerText);
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use indexOf. If the indexOf that item in the array you're trying to push to is -1, that means it doesn't exist, and that you can go ahead and push it in. Otherwise, do nothing. You can also reverse this, and add to the array from the other if it already exists, and do nothing if it doesn't.

Example:

const array = [1, 2, 3, 4, 5, 6, 7, 8];

// We are adding this. Expecting to not add the numbers that are already there
const toPushTo = [1, 10, 5];

const addToArrayIfNotDuplicate = (arr)=> {
  arr.forEach(item=>{
    toPushTo.indexOf(item) === -1 ? toPushTo.push(item) : null;
  })
};

addToArrayIfNotDuplicate(array);
console.log(toPushTo);

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