Home > Net >  How can I iterate through an array?
How can I iterate through an array?

Time:01-05

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?

CodePudding user response:

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.

CodePudding user response:

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();
});
  •  Tags:  
  • Related