简体   繁体   English

使用 javascript 中的当前日期过滤 API 数据

[英]filter an API data with current date in javascript

this is example of data from my API look like.这是来自我的 API 的数据示例。

const data = [{
    "id": "1",
    "name": "Lesley",
    "creationDate": "2019-11-21 20:33:49.04",
},
{
    "id": "2",
    "name": "Claude",
    "creationDate": "2019-11-21 20:33:09.397",
},
{
    "id": "3",
    "name": "Lesley",
    "creationDate": "2019-11-20 20:31:46.357",
{
    "id": "4",
    "name": "Yin Sun Shin",
    "creationDate": "2019-11-20 23:13:40.41",
},
{
    "id": "5",
    "name": "Claude",
    "creationDate": "2019-11-21 23:13:30.18",
},
{
    "id": "6",
    "name": "Moskov",
    "creationDate": "2019-11-20 23:10:22.863",
},
{
    "id": "7",
    "name": "Lesley",
    "creationDate": "2019-11-19 01:15:26.457",
},
{
    "id": "8",
    "name": "Yin Sun Shin",
    "creationDate": "2019-11-19 19:39:32.233",
},
{
    "id": "9",
    "name": "Claude",
    "creationDate": "2019-11-18 19:38:54.117",
}]

i have a list of data that need to display all information in vue-ant-design list.我有一个需要在 vue-ant-design 列表中显示所有信息的数据列表。 but there is too much of data that make the system lagging.但是有太多的数据使系统滞后。 i'm intended filter this data before being displayed.我打算在显示之前过滤这些数据。 i've tried some other javascript function to display the latest date data but not succeed.我尝试了其他一些 javascript function 来显示最新的日期数据,但没有成功。 is there any javascript reference that i can refer or any sharing that i can refer for how to filter this API data with the latest date in the creationDate?是否有任何我可以参考的 javascript 参考或我可以参考的任何共享,以了解如何使用 creationDate 中的最新日期过滤此 API 数据? i've no more idea on how to filter this data.我不知道如何过滤这些数据。

The best will be to get the data from the API ready to show..最好的办法是从准备展示的 API 获取data

But, with JavaScript in the frontend you can do:但是,在前端使用 JavaScript 您可以:

  1. Sort by creationDate DESCcreationDate日期 DESC 排序
  2. Filter elements with creationDate.substring(0, 10) equal to first element's creationDate.substring(0, 10) in the sorted array过滤元素的creationDate.substring(0, 10)等于排序数组中第一个元素的creationDate.substring(0, 10)

Code:代码:

 const data = [{ "id": "1", "name": "Lesley", "creationDate": "2019-11-21 20:33:49.04", }, { "id": "2", "name": "Claude", "creationDate": "2019-11-21 20:33:09.397", }, { "id": "3", "name": "Lesley", "creationDate": "2019-11-20 20:31:46.357", }, { "id": "4", "name": "Yin Sun Shin", "creationDate": "2019-11-20 23:13:40.41", }, { "id": "5", "name": "Claude", "creationDate": "2019-11-21 23:13:30.18", }, { "id": "6", "name": "Moskov", "creationDate": "2019-11-20 23:10:22.863", }, { "id": "7", "name": "Lesley", "creationDate": "2019-11-19 01:15:26.457", }, { "id": "8", "name": "Yin Sun Shin", "creationDate": "2019-11-19 19:39:32.233", }, { "id": "9", "name": "Claude", "creationDate": "2019-11-18 19:38:54.117", }] const result = data.sort((a, b) => new Date(b.creationDate) - new Date(a.creationDate)).filter((a, _, arr) => a.creationDate.substring(0, 10) === arr[0].creationDate.substring(0, 10)) console.log(result)

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

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