-1
    export class ResCheck{
  descrizioneIntervento: string;
  destinazioneDescrizione: string;
  fermoLocalita: string;
  caricoData: string;
  veicoloTarga: string; 
  destinazione: string;
  }

I have this type of array and the caricoData is the value that contains the date that I need to verify. Basically, what I need to do is a button that, when clicked, is able to search through each option of the array for the element with the latest date in caricoData, and then print it on screen.

let formattedDate = Utilities.toDateTimeString(check[0].caricoData)

Right now, this only get the date of the first element regardless of how late the date is, check[] being the array to check.

All I ask is how can I determine which of the element in the array has the latest date? I thought abotu using a forEach() but I can't seem to make it work in any way. The date is saved as DD/MM/YYYY

  • `check[0]` will always return the first element of the array... – pilchard May 12 '22 at 10:02
  • 1
    Does this answer your question? [Filter object array to return the object with the latest date property](https://stackoverflow.com/questions/35202163/filter-object-array-to-return-the-object-with-the-latest-date-property) – pilchard May 12 '22 at 10:03
  • You could sort the array and get the first item to get the most recent element: https://jsfiddle.net/whxm5jd8/ although this sounds like a duplicate of the question linked by @pilchard – briosheje May 12 '22 at 10:17
  • @pilchard it didn't, result is "invalid" – Lorenzo Bertolaccini May 12 '22 at 10:22
  • You can't just expect to copy and paste, but all of the answers address your issue save for parsing your date: [How to convert dd/mm/yyyy string into JavaScript Date object?](https://stackoverflow.com/questions/33299687/how-to-convert-dd-mm-yyyy-string-into-javascript-date-object) – pilchard May 12 '22 at 10:24
  • Also see: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – pilchard May 12 '22 at 10:25

0 Answers0