2014-04-17 127 views
0

使用cheerio,我设法凑包含日期,地点等。行数是可变的列一个PHP生成的表,我选择使用.MAP()来遍历每行使用提供的CSS选择器设置匹配的开始事件日期(startDate)。上述过程似乎运行良好,因为当我打电话给console.log(startDate)时,我收到下面的输出。但是,它会出现,但该进程创建一个数组,每次它移动到下一行时,每次追加一个附加日期。我怎样才能设置一个变量只有数组startDate中的最后一个数组?如何获得最后一个数组

[ '03/18/2014' ] 
[ '03/18/2014', '03/01/2014' ] 
[ '03/18/2014', '03/01/2014', '02/15/2014' ] 
[ '03/18/2014', '03/01/2014', '02/15/2014', '01/31/2014' ] 
[ '03/18/2014', 
    '03/01/2014', 
    '02/15/2014', 
    '01/31/2014', 
    '01/17/2014' ] 
[ '03/18/2014', 
    '03/01/2014', 
    '02/15/2014', 
    '01/31/2014', 
    '01/17/2014', 
    '12/06/2013' ] 
[ '03/18/2014', 
    '03/01/2014', 
    '02/15/2014', 
    '01/31/2014', 
    '01/17/2014', 
    '12/06/2013', 
    '11/16/2013' ] 

这样的console.log的期望输出(newArray)将是:

[ '03/18/2014', 
    '03/01/2014', 
    '02/15/2014', 
    '01/31/2014', 
    '01/17/2014', 
    '12/06/2013', 
    '11/16/2013' ] 

回答

1

如果的startDate是一个数组,你应该能够得到数组中的最后一个项目使用索引,像这样:

var lastStartDate = startDate[startDate.length-1]; //now lastStartDate contains the last item in the array 
+0

跟进:这样看来,这导致输出'03 /二千零十四分之十八 2014年3月1日 2014年2月15日 2014年1月31日 01/17/2014 12/06/2013 11/16/2013'有反正我可以输出'[03/18/2014, 03/01/2014, 02/15/2014, 2014年1月31日, 2014年1月17日, 2013年12月6日, 2013年11月16日“]” –

+0

你们是不是要输出成一个字符串,加入用逗号? – attila

+0

一个数组,用逗号分隔各个字符串(日期)。从我可以收集的内容来看,使用startDate [startDate.length-1]正在输出7个字符串,但该输出未连接到数组中。我希望能够输出一个数组,如上所述,而不是简单地输出最后一个数组中的元素。 –

相关问题