Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!