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

154
Visualizações
Why is findIndex() returning -1 here?
if(e.target.className === "deleteNode") {
        console.log(e.target.parentNode);
        let index = allNodes.findIndex(el => {
            console.log("E: "+e.target.parentNode.lastChild.innerHTML);
            console.log("El: "+el.lastChild.innerHTML);
            console.log("compare: "+el.lastChild.innerHTML.localeCompare(e.target.parentNode.lastChild.innerHTML));
            el.lastChild.innerHTML.localeCompare(e.target.parentNode.lastChild.innerHTML))
        });
        console.log("index: "+index);

The above is called from a click listener. Click happens on a button inside a dynamically created div element. The last child of the div is a <p> node. I want to get the index of the first match for the value of the text inside that div. allNodes is an array declared at the top of the script.

I have tried using the === comparator but same result. The 2 strings are identical but findIndex() is failing (returning -1)

console output for the above segment below enter image description here

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

0

There is no auto return from arrow functions when you use {} like that. You have to use the return statement.

if(e.target.className === "deleteNode") {
        console.log(e.target.parentNode);
        let index = allNodes.findIndex(el => {
            console.log("E: "+e.target.parentNode.lastChild.innerHTML);
            console.log("El: "+el.lastChild.innerHTML);
            console.log("compare: "+el.lastChild.innerHTML.localeCompare(e.target.parentNode.lastChild.innerHTML));
            return el.lastChild.innerHTML.localeCompare(e.target.parentNode.lastChild.innerHTML))
        });
        console.log("index: "+index);
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