Filter() not returning correct data

I am trying to filter a data-set based on date. But I keep getting an empty array returned. Can someone help!

Structure of large data-set (named: MFData):

[
   {
      Active Borrowers: 0   
      Date: Thu Jun 30 2011 00:00:00 GMT-0400 (Eastern Daylight Time) {}
      District: "Awaran"
      Province: "Baluchistan"
   },
   {
      ......
    }
....
]

Code to pull out only data that meets filter criteria:

vis.filteredData = vis.MFData.filter(d => {
    d["Date"] === new Date("2016/6/30");
  });

Expected: not empty array should be returned.
Actual: empty array returned.

I tried changing the date and the field on which I am filtering in case there was a variable type mismatch with the dates. Nothing worked.

Help!

Thank you.

A Date is an object, and when you attempt to compare two objects they are only considered equal if they are the same object (as opposed to just having the same values).

const date1 = new Date("2016/6/30");
const date2 = new Date("2016/6/30");

date1 === date2 // false
1 Like

from here you could see what to start researching, there are various answers that give various alternative on how to go here

1 Like

I’m still a little lost. Result is still empty array. This is the code I tried:

//Select year to display:
  vis.filteredData = vis.MFData.filter(d => {
    // console.log(d["Date"].getTime());
    // console.log(endDate.getTime());
    new Date(d["Date"]).getTime() <= new Date(endDate).getTime();
  });

and

//Select year to display:
  vis.filteredData = vis.MFData.filter(d => {
    // console.log(d["Date"].getTime());
    // console.log(endDate.getTime());
    d["Date"].getTime() <= endDate.getTime();
  });

What do the console.log statement display to the console? Is it what you expect? Based on the date value I see in the JSON you posted above, these are not just dates but also date/time. If end date is 2019-05-01, then this assumes 00:00:00 UTC, so my guess is that the dates are all greater than the end date. My guess is you probably should be only comparing the month, day, and year of the two date instead of the time, because if they are off by a second, then you may not capture them in the filter.

If you can show us the data array with all the times and tell us what end date you are wanting to use AND tell us what you expect to see in the filtered array, then we can better assist you.

1 Like

Thanks @RandellDawson.

So these are the results of the console.log() statements:

console.log(d["Date"].getTime());
//1309406400000 (each of these is returned 148 times; 1 per row in dataset)
//1341028800000
//1372564800000
//1404100800000
//1435636800000
//1467259200000
//1498795200000
//1530331200000
//1561867200000

Those correspond to all the dates in the data-set which start at June 30 2011 and end on June 30 2019.

For endDate.getTime(), I randomly set the slider to June 13, 2015 which translated to:
//1434189600000

[When I tried inputting “2015/6/13” in here, I got a slightly different number:
//1434168000000
This is off but shouldn’t I still get some dates filtered out…? Since it’s clearly a smaller number than some of the ones returned from d["Date"].getTime()…?

Also, this is what raw d["Date"] and endDate look like:

Sun Jun 30 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
Sat Jun 13 2015 06:00:00 GMT-0400 (Eastern Daylight Time)

I also updated my code to GitHub: https://github.com/SabahatPK/RoundTwo/blob/master/js/mainMap.js

The relevant code is in the wrangleData() method.

Hi @RandellDawson - I have still not been able to solve this…if you have time to review my last response, I’d really appreciate it. Thank you in advance!