a JS newbie question about logical operators

Hi Community!

I’ve read the MDN web docs on logical operators
as well as several other online resources, but I am still wrestling with this one…

I am trying to filter through a list of cases to figure out which ones were active during this financial year… but I keep failing. Specifically, I’d like to use the following logic:

  1. If a case came into the system after X date, and it’s still open, add it to the list.
  2. If a case came in to the system after X date, and closed before Y date, add it to the list
  3. Forget everything else.

I’ve tried this:

cases_handled_fy19 = case_data.filter(function(case_data) {
  return Date.parse(case_data.closure_date) < case_data.case_status == "Open" ||
         Date.parse(case_data.closure_date) > Date.parse(2018, 6, 30) &&
         Date.parse(case_data.closure_date) < Date.parse(2019, 7, 1)
})

… but the result is incorrect.

Narrowing it down:

cases_handled_fy19_alt2 = case_data.filter(function(case_data) {
  return Date.parse(case_data.closure_date) < Date.parse(2019, 6, 1) && case_data.case_status == "Open"
})

… returns 0 objects, yet I know that the data has open cases that were received before July 01, 2019

Here’s a link:

Would someone be willing to help me understand how to write this function? I appreciate this might be the wrong place to ask (is it’s not an observable-specific question). Please forgive (or kindly reprimand me for future reference).

Thank you!!

1 Like

I think the following explains the result of your “narrowed-down” query: the data objects in your array which have case_status equal to "Open" have their closure_date fields equal to "open", not a date:

case_data.filter(d => d.case_status == "Open").map(d => d.closure_date)
2 Likes

Thanks Bryan. Turns out that I was chasing down the wrong rabbit hole!

In sum: My attempted filtering didn’t work not because the formula was off, but because I was pointing it to an incorrect place (closure date rather than to ask - if received before this date).

I’ll should try to work harder at a solution. I do appreciate being able to ask questions when I get stuck. Thank you for always being so fast and generous with your help!

No problem Aaron! Getting filter functions right can be tricky… it feels like it always takes me a few tries. Fortunately, Observable makes it easy to quickly inspect the data when something doesn’t go as expected.

1 Like