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

Viewed 77

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?

5 Answers

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.

   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.

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