• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

191
Views
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?

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

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.

almost 3 years ago · Juan Pablo Isaza Report

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();
});
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error