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

213
Vistas
How can I iterate through an array?

I have an array called tables that I need to iterate through, but in my current code it is iterating and then stopping at the first index of the array ignoring the rest of the values in the array.

My code:

async function scrapeSiteYield(){

    var yield = [];
    var country = [];
    
    const yieldResult = await axios.get("https://tradingeconomics.com/forecast/government-bond-10y");

    const $ = cheerio.load(yieldResult.data);
    var tables = ["8","12","16","20","24"];
    var count = 1;
    tables.forEach(function (item, index) {
        var scrapeStatus = false;
        while (!scrapeStatus) {
            var res = {};
            $(`#aspnetForm > div.container > div > div > div:nth-child(${item}) > div > table > tbody > tr:nth-child(${count}) > td.datatable-item-first`).each((index, element) => {
                console.log(item);
                console.log($(element).text().trim());
                res.country = $(element).text().trim();
            });
            if (res.country == null || res.country === undefined) {
                scrapeStatus == true;
                break;
            }
            else{
                count++;
                yield.push(res);
            }
        }
      });
}
scrapeSiteYield();

My result:

8
UK
8
Germany
8
Russia
8
Italy
8
France
8
Switzerland
8
Czech Republic
8
Ireland
8
Portugal

So the values 12,16,20,24 in my table array is not being iterated through, how can I fix this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You actually never assign true to scrapeStatus.

This code

            if (res.country == null || res.country === undefined) {
                scrapeStatus == true;
                break;
            }

Should look like this:

            if (res.country == null || res.country === undefined) {
                scrapeStatus = true;
            }

1- The while clause will break when scrapeStatus is true according to your code. No need to add a break.

2- Assign true to scrapeStatus instead of checking if it's true.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have two loops. The jQuery loop each() is inside the tables.forEach() loop, meaning that it will loop through all elements with the same item from tables array.

To fix this you need to itirate the tables array inside the each() loop, something like so:

elements.each((index, element) => {
    let tableIndex = 0;

    if(index < tables.length){
       tableIndex = index;
    }
    console.log(tables[tableIndex]);
    console.log($(element).text().trim());
    res.country = $(element).text().trim();
});
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