简体   繁体   English

如何获取最后一个数组

[英]How to Get Last Array

Using cheerio, I have managed to scrape a PHP generated table that contains a column of dates, locations, etc. As the number of rows is variable, I opted to use .map() to iterate through each row, setting the matching starting event dates (startDate) with the provided CSS selectors. 使用cheerio,我设法抓取了一个包含日期,位置等列的PHP生成的表。由于行数是可变的,我选择使用.map()遍历每行,设置匹配的开始事件日期(startDate)与提供的CSS选择器。 The above process seems to be functioning well, as when I call console.log(startDate) I recieve the output below. 上面的过程似乎运行良好,因为当我调用console.log(startDate)时,我收到以下输出。 it would appear, however, that the process creates an array for each time it moves to the next row, appending an additional date each time. 但是,该过程似乎会在每次移至下一行时创建一个数组,每次都附加一个附加日期。 How can I set a variable to only the last array in the array 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' ]

So the desired output of console.log(newArray) would be: 因此,console.log(newArray)的期望输出为:

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

如果startDate是一个数组,则应该能够通过使用索引来获取数组中的最后一项,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM