-2
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

Philip Butler
  • 479
  • 1
  • 5
  • 13
  • it's cz you are not returning anything from function – Shyam Oct 08 '21 at 18:57
  • i did not close the issue ... and return is not automatic. if you use curly braces then you have to return if you are using round brackets it's auto return – Shyam Oct 08 '21 at 19:01

1 Answers1

1

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);
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39