Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda