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

135
Views
For loop breaks an unrelated array

Hey so I am using puppeteer and when I run this code:

console.log(dealsId[i]);

for (var i = 0; i < sizes.length; i++) {
    var refIdClasses = await sizes[i].$eval('input', a => a.getAttribute('class'));
    if (refIdClasses != 'disabled') {
        var refId = await sizes[i].$eval('input', a => a.getAttribute('value'));
        var size = await sizes[i].$eval('input', a => a.getAttribute('data-size'));
        refIds.push({ size: size, refId: refId });
    }
}
console.log(dealsId[i]);

The Deals Id works before the for loop and after the loop it says undefined and I am so confused on why.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Var i is globally-scoped, so by using var i=0 you are definitely creating scope conflict. Use let or another variable name instead.

about 4 years ago · Juan Pablo Isaza Report

0

That's the problem with vars. You can declare and re-declare them in the same scope without warning.

I assume you declared a var i in the code before what you showed, but you re-declare it in the for loop. i < sizes.length being the condition to break the loop, the loop break and i is now out-of-bounds of your array, hence the undefined (if dealsId has a size that's inferior to sizes array).

It isn't "unrelated" since you used the same variable to loop through 2 different arrays. Since we don't know if the two arrays have the exact same size, we can't pinpoint with 100% accuracy what's going on, but I'll go with that.

about 4 years ago · Juan Pablo Isaza Report

0

This is because your for loop is also using i to iterate, possibly conflicting with the i in dealsId[i].

Possible solution might be to change the variable in for loop from i to j.

The code becomes:

console.log(dealsId[i]);

for (var j = 0; j < sizes.length; j++) {
    var refIdClasses = await sizes[j].$eval('input', a => a.getAttribute('class'));
    if (refIdClasses != 'disabled') {
        var refId = await sizes[j].$eval('input', a => a.getAttribute('value'));
        var size = await sizes[j].$eval('input', a => a.getAttribute('data-size'));
        refIds.push({ size: size, refId: refId });
    }
}
console.log(dealsId[i]);
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!