我的 VueJs 页面中有一个数据表,并通过 ajax 请求获取其数据。返回数据是这样的
{
name: "Test Name",
created_at: "2019-06-14 07:31:15"
}
我有一些按钮,应该使用以下规则过滤数据:
Created data within the last 5 mins
Created data within the last 15 mins
Created data within the last 30 mins
鉴于我的created_at
,我该如何执行此操作?我目前有这个功能
computed: {
filterData() {
return this.customers.filter(item => item.created_at.includes(new Date().getDay()+1));
}
}
数据this.customers
是我来自ajax请求的数据。
我只是从这里引用它 filter response data date wise vue 。基于此,我希望数据将按 1 天前的数据进行过滤。但这对我来说也不起作用。也许是因为我的日期是日期时间而不是日期?
最佳答案
如果你的日期格式可以被默认的 JS Date 函数解析,你可以尝试下面的代码。两个日期之间的减号 (-) 将给出以毫秒为单位的差异。除以 60 * 1000 得出以分钟为单位的差异。
a = [{
name: "Test Name",
created_at: "2019-09-29 10:00:15"
},
{
name: "Test Name",
created_at: "2019-09-29 10:21:15"
},
{
name: "Test Name",
created_at: "2019-09-29 09:21:15"
},
{
name: "Test Name",
created_at: "2019-09-29 09:50:15"
}]
const filterByLastMins = (mins, arr) => {
const currentTime = new Date();
return arr.filter((elem) => {
const diffInMins = (currentTime - Date.parse(elem.created_at)) / (1000 * 60);
return diffInMins < mins
})
}
res = filterByLastMins(60, a);
console.log(res);
关于javascript - 按分钟过滤数据 VueJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152074/