CasperJS 响应数组的每个然后下一个(未来)值
CasperJS each then next (future) value of response array
我正在使用 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
问题出现在循环结束时,因为我实际与上一个进行比较,最后一组不会生成任何文件(当然,最后2个项目将具有相同的[0],然后循环将结束)
我这里的解决方案是要求下一项而不是前一项,并在每次迭代的末尾生成文件,但我不知道如何告诉 Casper给我数组中的 actualItem+1 项。
我将尝试解决此问题,即在 paralel 中迭代相同的数组并返回 actual+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 分配给响应之前要求创建文件,然后在最后再次询问最后一项。
希望对您有所帮助
我正在使用 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
问题出现在循环结束时,因为我实际与上一个进行比较,最后一组不会生成任何文件(当然,最后2个项目将具有相同的[0],然后循环将结束)
我这里的解决方案是要求下一项而不是前一项,并在每次迭代的末尾生成文件,但我不知道如何告诉 Casper给我数组中的 actualItem+1 项。
我将尝试解决此问题,即在 paralel 中迭代相同的数组并返回 actual+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 分配给响应之前要求创建文件,然后在最后再次询问最后一项。
希望对您有所帮助