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

233
Visualizações
how to skip queryselectorall does not exist or null
for (i = 0; i < document.querySelectorAll('span.movie-cast-title').length; i++)
{
  if (document.querySelectorAll('span.gcharacter')[i].innerText, == 'null') {
    continue;
  }
  data.actor.push({
    "@type": "PerformanceRole",
    "actor": {
      "@type": "Person",
      "name": document.querySelectorAll('span.movie-cast-title')[i].innerText,
      "url": document.querySelectorAll('a.movie-cast-url')[i].href,
    }
    "characterName": document.querySelectorAll('span.gcharacter')[i].innerText,
  });
}

how to skip queryselectorall does not exist or null "characterName": document.querySelectorAll('span.gcharacter')[i].innerText

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

0

You need to check if the index, that is, i is within the bounds of querySelectorAll:

if(i >= document.querySelectorAll('span.gcharacter').length) {
    continue;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to check if the element exists, not the innerText. Because if the element doesn't exist, you can't get its innerText.

if (!document.querySelectorAll('span.gcharacter')[i]) {
    continue;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

wouldn't it be clearer to use a forEach?

const 
  spCharacters = document.querySelectorAll('span.gcharacter')
, aMovieCast   = document.querySelectorAll('a.movie-cast-url')
  ;
document.querySelectorAll('span.movie-cast-title').forEach( (sp,i) =>
  {
  if (spCharacters[i].innerText !== 'null')
    {
    data.actor.push(
      { '@type' : 'PerformanceRole'
      , 'actor' :
        { '@type' : 'Person'
        , 'name'  : sp.innerText,
        , 'url'   : aMovieCast[i].href,
        }
      , 'characterName': spCharacters[i].innerText
      });
    }
  }
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