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

210
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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