Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda