简体   繁体   English

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

[英]How to filter an array of objects based on whether the range between their date properties includes a given date?

How do I filter date-ranges based on whether they include a specific 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"
}];

I want to filter the data above by the following date:我想在以下日期之前过滤上面的数据:

let knowntime = "2020-04-02 13:20:22";

This is the result I'm looking for:这是我正在寻找的结果:

let filtered = [{
  "S.NO": 3,
  "Full Name": "Doll",
  "Check In": "2020-04-02 11:19:48",
  "Check Out": "2020-04-02 15:19:58"
}];

How do I do that?我怎么做?

 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)

 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);

You could alternatively write a one-liner like this:你也可以像这样写一个单行:

let filtered = visitors.filter(({ ['Check In']: a, ['Check Out']: b }) => a < knowntime && b > knowntime);

To filter the visitor data, first make sure that both the properties have values inside them, then convert the values in date format using new Date() and then compare.要过滤访问者数据,首先确保两个属性内部都有值,然后使用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)

 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);

From the data, it is clear that there could be a chance for having the empty values for check out time.从数据中可以清楚地看出,退房时间可能有空值。 Should you consider the Check Out is greater than the given time if it is empty.如果 Check Out 为空,您是否认为 Check Out 大于给定时间。

Here is the code for that这是代码

   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)

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

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