如何根据日期属性之间的范围是否包含给定日期来过滤对象数组?

如何根据日期范围是否包含特定日期来过滤日期范围?


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 = [{

  "S.NO": 3,

  "Full Name": "Doll",

  "Check In": "2020-04-02 11:19:48",

  "Check Out": "2020-04-02 15:19:58"

}];

我怎么做?


心有法竹
浏览 239回答 5
5回答

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=[&nbsp; {&nbsp; &nbsp; "S.NO": 1,&nbsp; &nbsp; "Full Name": "Boss",&nbsp; &nbsp; "Check In": "2020-04-21 01:51:49",&nbsp; &nbsp; "Check Out": ""&nbsp; },&nbsp; {&nbsp; &nbsp; "S.NO": 2,&nbsp; &nbsp; "Full Name": "John",&nbsp; &nbsp; "Check In": "2020-04-19 11:21:17",&nbsp; &nbsp; "Check Out": "2020-04-19 12:21:26"&nbsp; },&nbsp; {&nbsp; &nbsp; "S.NO": 3,&nbsp; &nbsp; "Full Name": "Doll",&nbsp; &nbsp; "Check In": "2020-04-02 11:19:48",&nbsp; &nbsp; "Check Out": "2020-04-02 15:19:58"&nbsp; }];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 大于给定时间。这是代码&nbsp; &nbsp;let visitors = [&nbsp; {&nbsp; &nbsp; "S.NO": 1,&nbsp; &nbsp; "Full Name": "Boss",&nbsp; &nbsp; "Check In": "2020-04-21 01:51:49",&nbsp; &nbsp; "Check Out": ""&nbsp; },&nbsp; {&nbsp; &nbsp; "S.NO": 2,&nbsp; &nbsp; "Full Name": "John",&nbsp; &nbsp; "Check In": "2020-04-19 11:21:17",&nbsp; &nbsp; "Check Out": "2020-04-19 12:21:26"&nbsp; },&nbsp; {&nbsp; &nbsp; "S.NO": 3,&nbsp; &nbsp; "Full Name": "Doll",&nbsp; &nbsp; "Check In": "2020-04-02 11:19:48",&nbsp; &nbsp; "Check Out": "2020-04-02 15:19:58"&nbsp; }];let knowntime = "2020-04-02 13:20:22";let filteredVisitors =&nbsp; visitors.filter((visitor) => {&nbsp; 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 = [{&nbsp; "S.NO": 1,&nbsp; "Full Name": "Boss",&nbsp; "Check In": "2020-04-21 01:51:49",&nbsp; "Check Out": ""}, {&nbsp; "S.NO": 2,&nbsp; "Full Name": "John",&nbsp; "Check In": "2020-04-19 11:21:17",&nbsp; "Check Out": "2020-04-19 12:21:26"}, {&nbsp; "S.NO": 3,&nbsp; "Full Name": "Doll",&nbsp; "Check In": "2020-04-02 11:19:48",&nbsp; "Check Out": "2020-04-02 15:19:58"}];let knowntime = "2020-04-02 13:20:22";let filtered = visitors.filter(visitor => {&nbsp; return visitor['Check In'] < knowntime && visitor['Check Out'] > knowntime;});console.log(filtered);您也可以像这样写一个单行代码:let&nbsp;filtered&nbsp;=&nbsp;visitors.filter(({&nbsp;['Check&nbsp;In']:&nbsp;a,&nbsp;['Check&nbsp;Out']:&nbsp;b&nbsp;})&nbsp;=>&nbsp;a&nbsp;<&nbsp;knowntime&nbsp;&&&nbsp;b&nbsp;>&nbsp;knowntime);

有只小跳蛙

要过滤访问者数据,首先确保两个属性都包含值,然后将值转换为日期格式new Date(),然后进行比较。&nbsp; &nbsp;let visitors = [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "S.NO": 1,&nbsp; &nbsp; &nbsp; &nbsp; "Full Name": "Boss",&nbsp; &nbsp; &nbsp; &nbsp; "Check In": "2020-04-21 01:51:49",&nbsp; &nbsp; &nbsp; &nbsp; "Check Out": ""&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "S.NO": 2,&nbsp; &nbsp; &nbsp; &nbsp; "Full Name": "John",&nbsp; &nbsp; &nbsp; &nbsp; "Check In": "2020-04-19 11:21:17",&nbsp; &nbsp; &nbsp; &nbsp; "Check Out": "2020-04-19 12:21:26"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "S.NO": 3,&nbsp; &nbsp; &nbsp; &nbsp; "Full Name": "Doll",&nbsp; &nbsp; &nbsp; &nbsp; "Check In": "2020-04-02 11:19:48",&nbsp; &nbsp; &nbsp; &nbsp; "Check Out": "2020-04-02 15:19:58"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ];&nbsp; &nbsp; let knowntime = "2020-04-02 13:20:22";&nbsp; &nbsp; let filteredVisitors =&nbsp; visitors.filter((visitor) => {&nbsp; &nbsp; &nbsp; return (visitor['Check In'] && visitor['Check Out'] &&&nbsp; &nbsp; &nbsp; &nbsp; (new Date(visitor['Check In']) <= new Date(knowntime) && new Date(visitor['Check Out']) >= new Date(knowntime)))&nbsp; &nbsp; })&nbsp; &nbsp; console.log(filteredVisitors)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript