简体   繁体   English

如何从 Nuxt.js / Vue.js 中的数组中获取具有第一个下一个日期的 object?

[英]How to get the object with the first next date from an array in Nuxt.js / Vue.js?

I want to get the object with the first next date from an array in Nuxt.js.我想从 Nuxt.js 的数组中获取具有第一个下一个日期的 object。

I'm showing the object manually with the method below in the computed section:我在计算部分使用以下方法手动显示 object:

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.我想检查数组中的对象,看看下一个要获取的是哪个 object。

Tried with @nuxtjs/moment but I failed.尝试使用@nuxtjs/moment,但我失败了。

How can I solve this?我该如何解决这个问题?

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.您可以使用自定义排序 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 .另请参阅此StackOverflow 答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM