0

I want to get the object with the first next date from an array in Nuxt.js.

I'm showing the object manually with the method below in the computed section:

showObject(){
    const myData = Object.keys(this.fixtures).map(key => this.fixtures[key]);
    return myData.find(x => x.teams.home.uid === this.teamID && x._dt.date === '04/06/2020');
}

This means I have to keep changing the date.

I want to check the objects in the array and see which object is the next one to get.

Tried with @nuxtjs/moment but I failed.

How can I solve this?

Can
  • 380
  • 1
  • 21
  • What does *"first next date"* mean? And how does your data look like? – tao May 29 '20 at 23:17
  • you mean first next date from current date? – Karl L May 29 '20 at 23:53
  • I mean, if today is 30/05/20 And in the array there are objects with the date: 01/06/2020 04/06/2020 It need to return the first date > 01/06/2020 is the first date so I want to return the object with that date. – Can May 30 '20 at 11:16

1 Answers1

0

You need to bring your array into an order which relies on the sequence of the dates. You can do this with a custom sort function. Then, you can just increase the index by one and access the next element in the array.

Here is an example of how to sort the array by date.

const array = [{
  date: new Date(),
  value: 123
}, {
  date: new Date(),
  value: 321
}, {
  date: new Date(),
  value: 'test'
}];

console.log(array.sort((a, b) => {
  return a.date - b.date;
}));

See also this StackOverflow answer.

ssc-hrep3
  • 10,806
  • 4
  • 35
  • 77