简体   繁体   English

如何遍历数组?

[英]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.我有一个称为表的数组,我需要遍历它,但在我当前的代码中,它正在迭代,然后在数组的第一个索引处停止,忽略数组中值的 rest。

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?所以我的表数组中的值 12,16,20,24 没有被迭代,我该如何解决这个问题?

You actually never assign true to scrapeStatus .您实际上从未将true分配给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. 1- 根据您的代码,当scrapeStatus为 true 时, while子句将中断。 No need to add a break.无需添加休息。

2- Assign true to scrapeStatus instead of checking if it's true . 2-将true分配给scrapeStatus而不是检查它是否为true

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. jQuery 循环each()tables.forEach()循环内,这意味着它将遍历表数组中具有相同项目的所有元素。

To fix this you need to itirate the tables array inside the each() loop, something like so:要解决此问题,您需要在each()循环中迭代表数组,如下所示:

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM