Javascript:在循环内增加计数器变量

use*_*600 2 javascript for-loop node.js

我使用Selenium和nodejs迭代html表并将数组中的字符串与表格单元格中的字符串匹配.

我的数组可能是["Name","Address1","Address2","Address3",...],tr [1]中的值将尝试与Arr [0]匹配,tr [2]与Arr [ 1]等

html表对于数组中的每个项都有一行,但是如果没有数据,比如说Address2则不会出现.

在这种情况下,tr [3]会发现它与Arr [3]不匹配.而不是移动到tr [4],我想看看tr [3]是否与Arr [4]匹配,然后是Arr [5]等.表中的项目总是与数组中的项目顺序相同,所以我不需要任何"无与伦比"的数组项.

我发布了整个函数,以防它相关,但问题似乎很简单,因为我不能得到"i = i - 1"来将新值带入下一个循环迭代.

我已经证明我进入了Else部分,并且i - 1的值正如我在那时所期望的那样.

var retrieveData = function retrieveData(Arr){

j = 0

driver.findElements(webdriver.By.xpath("//*[@class='table-container']")).then (function(rows){

    rowCount = rows.length;
    console.log("Row count = " + rowCount);

}).then (function(fn){

    for (i = 1;i < rowCount + 1; i++){

        (function(i){

            var element = driver.findElement(webdriver.By.xpath("//div[@class='table-container']["+i+"]/div/strong/a"));

            element.getText().then(function(Type){

                var typefromArray = String(Arr[j].split(':')[0]);

                if (Type == typefromArray){

                    // Do something

                    j = j + 1;

                } else {

                    // Do something

                    i = i - 1 // My problem looks to be here, but may be in the transfer of this back up to the beginning of the loop       
                    j = j + 1;

                }

            });

        })(i);

    };

});

};

module.exports.retrieveData = retrieveData;
Run Code Online (Sandbox Code Playgroud)

lle*_*aff 5

您在传递索引的-loop 中使用了IIFEfor.看起来它的设计是为了防止修改i!

当你这样做

i = i - 1
Run Code Online (Sandbox Code Playgroud)

在函数结束时,它完全没有效果,因为它只影响你的函数i 内部.
这是一篇关于JavaScript中变量作用域的好文章.

但是如果你仍然想要修改i,那么一个选项就是让它由匿名函数返回并分配给外部i变量:

.then (function(fn){
    for (i = 1;i < rowCount + 1; i++){
        i = (function(i){
            var element = driver.findElement(webdriver.By.xpath("//div[@class='table-container']["+i+"]/div/strong/a"));
            element.getText().then(function(Type){
                var typefromArray = String(Arr[j].split(':')[0]);
                if (Type == typefromArray){
                    // Do something
                    j = j + 1;
                } else {
                    // Do something
                    i = i - 1 // My problem looks to be here, but may be in the transfer of this back up to the beginning of the loop       
                    j = j + 1;
                }
            });
            return i;
        })(i);
    };
Run Code Online (Sandbox Code Playgroud)

这是你问的问题的解决方案.


但是我很确定你的代码中会出现其他问题,因为你正在使用同步循环,但是在异步调用函数(传递给函数element.getText().then)中更新计数器.

我建议你研究一下如何处理JavaScript中的异步代码(你现在正在使用Promises)或者打开一个关于你要解决的更大问题的更普遍的问题,因为会有更多的设计障碍克服.

下面是您可能必须使用的一种模式的示例,用于处理多个Promise(不是为了复制粘贴,为简洁起见使用ES2015):

.then(function(fn) {
  Promise.all(
    Array(rowCount).fill(true).map((_, i) => i + 1) // => [1..rowCount]
      .map(i => {
        const element = driver.findElement(webdriver.By.xpath("//div[@class='table-container']["+i+"]/div/strong/a"));
        return element.getText();
      })
  // Here we have an array of Promises that we pass to Promise.all
  //   so we can wait until all promises are resolved before executing
  //   the following .then
  ).then(types => types.filter(type => type === typefromArray)
                       .map(type => { /* doSomething */ });
});
Run Code Online (Sandbox Code Playgroud)