Pergunta

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' ]
Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top