Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:循环内的增量计数器变量_Javascript_Node.js_For Loop - Fatal编程技术网

Javascript:循环内的增量计数器变量

Javascript:循环内的增量计数器变量,javascript,node.js,for-loop,Javascript,Node.js,For Loop,我使用Selenium和nodejs迭代html表,并将数组中的字符串与表单元格中的字符串进行匹配 我的数组可能是[“名称”、“地址1”、“地址2”、“地址3”、…],tr[1]中的值将尝试与Arr[0]匹配,tr[2]与Arr[1]等匹配 html表将为数组中的每个项都有一行,但是如果没有Address2的数据,那么就不会出现 在这种情况下,tr[3]将发现它无法与Arr[3]匹配。我不想转到tr[4],而是想看看tr[3]是否与Arr[4]匹配,然后是Arr[5]等等。表中的项目将始终与数组

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

我的数组可能是[“名称”、“地址1”、“地址2”、“地址3”、…],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;
var retrieveData=函数retrieveData(Arr){
j=0
findElements(webdriver.By.xpath(“/*[@class='table-container']”)。然后(函数(行){
rowCount=rows.length;
console.log(“行计数=”+rowCount);
}).然后(功能(fn){
对于(i=1;i
您正在将索引传递到的
for
-循环中使用。 这看起来是为了防止修改
i

当你这样做的时候

i = i - 1
在函数结束时,它完全没有效果,因为它只影响函数内部的
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);
    };

你的生活中有
i=i-1
。您也可以使用
i--
j++
。您好,谢谢您的快速回复!我只想在字符串不匹配时减少计数器(即if语句返回False)。我怎样才能把I=I-1(或I--)保持在Else之外呢?你在
(函数(I){
声明中隐藏了
I
的范围。感谢你花时间回答如此详细的问题。我还有很多东西要学!我已经研究过承诺,但对它们的理解远远超过了使用“.then”在调用任何代码之前阻止我的计数器变量递增。出于类似的原因,我将IIFE(我只知道它是一个“闭包”,因此误解了第一个响应)包装在它周围,因为输出到控制台给了我一个不正确的值,而没有它(尽管它工作)
.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 */ });
});