0

I have a array about Events. How can I found the most near date in array about Events, and return the "Next" event ?

I need a function that use a default parameter "today now" or specific date, and return the Next event. I will render the result in a template on Vue3 composition api.

const data = [
  {id: 1, date:'April 5 2022, 9:00hs', eventName:'dancing ballet', city:'Sao Paulo'},
  {id: 2, date:'April 15 2022, 19:00hs', eventName:'party friends', city:'Buenos Aires'},
  {id: 4, date:'April 25 2022, 8:00hs', eventName:'school test', city:'Sao Paulo'},
  {id: 5, date:'April 30 2022, 21:00hs', eventName:'dinner', city:'Sao Paulo'}
];

const nextEvent = ??
  
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • What have you attempted? "most near date" to what? Today? Filter your array for all dates after the target date, sort by date, then the first element should be the next one. – mykaf Mar 14 '22 at 15:04
  • The problem here is that your dates are in human language. In human language, Mars comes after April. Surely somewhere you have the same dates in computer/ISO format, which you should add to your data and use to sort the objects. – Jeremy Thille Mar 14 '22 at 15:06
  • Please visit the [help], take the [tour] to see what and [ask]. Do some research - [search SO for answers](https://www.google.com/search?q=javascript+nearest+OR+closest+date+site%3Astackoverflow.com) If you get stuck, post a [mcve] of your attempt, noting input and expected output using the `[<>]` [snippet editor](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do). – mplungjan Mar 14 '22 at 15:08

0 Answers0