每个CasperJS然后是响应数组的下一个(未来)值

CasperJS each then next (future) value of response array

本文关键字:下一个 未来 数组 CasperJS 然后 响应 每个      更新时间:2023-09-26

im使用CasperJS的eachThen()方法,我需要知道下一个迭代项的值。

我有这样的项目:

array = [
[22,"blah"],
[22,"blah"],
[23,"blah"],
[24,"blah"],
[24,"blah"],
[24,"blah"]
]

我正在为每组项目生成一个文件(按[0]分组)。

我的第一种方法是在迭代开始时将实际项目与上一个项目进行比较,如果[0]不同,则生成文件,如下所示:

//previousItem was initilizated = to actualItem
   casper.eachThen(array , function (response) {
            var actualItem = response.data
        casper.then(function(){
            if(actualItem[0] != previousItem[0]){
                var filename = previousItem[0]+"file.html"
                var f = fs.open(filename, 'a');
                f.write(stdFile);
                f.close();  
                previous = actualItem
            }
        });
//operations that prepares stdFile to be created

问题出现在周期结束时,因为与之前相比,最后一组将不会生成任何文件(当然,最后两个项目将具有相同的[0],之后周期将结束)

我在这里的解决方案是请求下一个项而不是上一个项,并在每次迭代的末尾生成文件,但我不知道如何告诉Casper在数组中给我actualItem+1的项。

我将尝试在parallel中迭代相同的数组并返回实际+1的值来解决这个问题,但也许有一种方法可以使用响应变量来完成。

结果必须是3个文件:22file.html、23file.html和24file.html有很多制作的请求

,所以我需要赢得我能赢得的任何一秒。

如果你有其他方法来实现这一点,请告诉我。

PS:对不起,我的英语不是我的母语

感谢

我的第一种方法太复杂了,可能是因为这些天我睡得不多。

这就是我所做的:

cajIndex = 0;
//this block generates the file if actualItem (or actualCajero is the same) is not undefined  and the actualItem[0] is not the same as the next item 
if (actualCajero != undefined)
        {
            if (actualCajero[0] != cajerosArray[cajIndex][0]){
                casper.then(function(){
                    var filename = "Ventas local"+" "+nombreLocal+"_"+actualDay[1]+"-"+actualDay[2]
                    stdFile = '<h1>'+filename+'</h1> <table border="1">'+ stdTable + '</table>';
                    this.echo("generando archivo "+filename);
                    var f = fs.open(filename+".xls", 'a');
                    f.write(stdFile);
                    f.close();  
                    stdTable = "";
                    stdFile = "";
                });
            }
        }
//after this i do my operations , ask again for file creating at the end but now if the next item is undefined, if it is undefined creates the file then
cajIndex++

这解决了所有问题,关键是在0处声明该索引,并在将actualItem分配给响应之前请求创建文件,然后在最后再次请求最后一个项。

希望它能帮助