for in loop resuming the "length" index of a nodlist, is that normal? any tips to solve this?

Asked

Viewed 32 times

0

as said the loop for in pecorre all indices and returns the indices, in this case they are (0,1,2), it is also returning the nodelist’s Dice length that I am pecorrendo, so when using the returned Dice to access another array, the code tries to recover the value of the Dice length(3), caused an error since this Index (3) is not defined`

for(let iBox in boxSlider){
        let boxMain = boxSlider[iBox];
        for(let iCol of colecao[(iBox)]){
        boxMain.innerHTML += `
        <a href="" class="pr">
            <img src="${iCol.imagem}" alt="">
            <span class="tittle">${iCol.nome}</span>
        </a>`;
        }
    }

Note: boxSlider is the nodlist

2 answers

0

NodeList attributes are treated as indexes by the loop.

Transform the NodeList in a Array in fact through the function Array.from:

for(let iBox in Array.from(boxSlider)){
  let boxMain = boxSlider[iBox];
  for(let iCol of colecao[(iBox)]){
  boxMain.innerHTML += `
  <a href="" class="pr">
      <img src="${iCol.imagem}" alt="">
      <span class="tittle">${iCol.nome}</span>
  </a>`;
  }
}

0

Use a for simple that you have control of the content

for (let iBox = 0; i < boxSlider.length; iBox++) {
    let boxMain = boxSlider[iBox];
    // ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.