简体   繁体   中英

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. The above process seems to be functioning well, as when I call console.log(startDate) I recieve the output below. 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?

[ '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:

[ '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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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