【问题标题】:How to filter an array of objects based on whether the range between their date properties includes a given date?如何根据日期属性之间的范围是否包括给定日期来过滤对象数组?
【发布时间】:2020-11-15 00:08:12
【问题描述】:

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

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"
}];

我该怎么做?

【问题讨论】:

    标签: javascript json sorting date filter


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      要过滤访问者数据,首先确保两个属性中都有值,然后使用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)

      【讨论】:

        【解决方案3】:

        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)

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            【解决方案5】:

            从数据中可以清楚地看出,退房时间可能存在空值。如果 Check Out 为空,您是否应该认为 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)
            

            【讨论】:

              猜你喜欢
              • 2018-09-27
              • 1970-01-01
              • 2017-12-20
              • 2013-03-07
              • 2016-11-25
              • 2020-02-15
              • 1970-01-01
              • 2021-08-13
              • 1970-01-01
              相关资源
              最近更新 更多