문제

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' ]
도움이 되었습니까?

해결책

If startDate is an array, you should be able to get the last item in the array by using the index, like this:

var lastStartDate = startDate[startDate.length-1]; //now lastStartDate contains the last item in the array
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top