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

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

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 Report

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 Report

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