-
qq_笑_17
let visitors=[ { "S.NO": 1, "Full Name": "Boss", "Check In": "2020-04-21 01:51:49", "Check Out": "" }, { "S.NO": 2, "Full Name": "John", "Check In": "2020-04-19 11:21:17", "Check Out": "2020-04-19 12:21:26" }, { "S.NO": 3, "Full Name": "Doll", "Check In": "2020-04-02 11:19:48", "Check Out": "2020-04-02 15:19:58" } ];let knowntime="2020-04-02 13:20:22";res=visitors.filter(o => o["Check In"].localeCompare(knowntime) < 0 && o["Check Out"].localeCompare(knowntime)>0)console.log(res)
-
慕侠2389804
let visitors=[ { "S.NO": 1, "Full Name": "Boss", "Check In": "2020-04-21 01:51:49", "Check Out": "" }, { "S.NO": 2, "Full Name": "John", "Check In": "2020-04-19 11:21:17", "Check Out": "2020-04-19 12:21:26" }, { "S.NO": 3, "Full Name": "Doll", "Check In": "2020-04-02 11:19:48", "Check Out": "2020-04-02 15:19:58" }];let knowntime="2020-04-02 13:20:22";const dateToBeFiltered=new Date(knowntime);const filterdVisitors= visitors.filter((elem)=>{const checinTime=new Date(elem["Check In"]);const checkOutTime=new Date(elem["Check Out"]);return (checinTime<=dateToBeFiltered && dateToBeFiltered<=checkOutTime);});console.log(filterdVisitors);
-
噜噜哒
从数据中可以清楚地看出,结账时间可能有空值。如果它是空的,您是否应该考虑 Check Out 大于给定时间。这是代码 let visitors = [ { "S.NO": 1, "Full Name": "Boss", "Check In": "2020-04-21 01:51:49", "Check Out": "" }, { "S.NO": 2, "Full Name": "John", "Check In": "2020-04-19 11:21:17", "Check Out": "2020-04-19 12:21:26" }, { "S.NO": 3, "Full Name": "Doll", "Check In": "2020-04-02 11:19:48", "Check Out": "2020-04-02 15:19:58" }];let knowntime = "2020-04-02 13:20:22";let filteredVisitors = visitors.filter((visitor) => { return (new Date(visitor['Check In']) <= new Date(knowntime) && (visitor['Check Out'] === '' || new Date(visitor['Check Out']) >= new Date(knowntime)))})console.log(filteredVisitors)
-
动漫人物
let visitors = [{ "S.NO": 1, "Full Name": "Boss", "Check In": "2020-04-21 01:51:49", "Check Out": ""}, { "S.NO": 2, "Full Name": "John", "Check In": "2020-04-19 11:21:17", "Check Out": "2020-04-19 12:21:26"}, { "S.NO": 3, "Full Name": "Doll", "Check In": "2020-04-02 11:19:48", "Check Out": "2020-04-02 15:19:58"}];let knowntime = "2020-04-02 13:20:22";let filtered = visitors.filter(visitor => { return visitor['Check In'] < knowntime && visitor['Check Out'] > knowntime;});console.log(filtered);您也可以像这样写一个单行代码:let filtered = visitors.filter(({ ['Check In']: a, ['Check Out']: b }) => a < knowntime && b > knowntime);
-
有只小跳蛙
要过滤访问者数据,首先确保两个属性都包含值,然后将值转换为日期格式new Date(),然后进行比较。 let visitors = [ { "S.NO": 1, "Full Name": "Boss", "Check In": "2020-04-21 01:51:49", "Check Out": "" }, { "S.NO": 2, "Full Name": "John", "Check In": "2020-04-19 11:21:17", "Check Out": "2020-04-19 12:21:26" }, { "S.NO": 3, "Full Name": "Doll", "Check In": "2020-04-02 11:19:48", "Check Out": "2020-04-02 15:19:58" } ]; let knowntime = "2020-04-02 13:20:22"; let filteredVisitors = visitors.filter((visitor) => { return (visitor['Check In'] && visitor['Check Out'] && (new Date(visitor['Check In']) <= new Date(knowntime) && new Date(visitor['Check Out']) >= new Date(knowntime))) }) console.log(filteredVisitors)