I'm having trouble filtering an API array to only return objects with a start time from the past 24 hours. I've cleaned up the time data from the API and converted it to unix time, and am trying to use that to filter out anything beyond the past 24 hours.
However, when I attempt to filter the array using this criteria, it returns blank. Been rather stumped on this for awhile. Can anyone point me in the right direction? Please and thanks.
app.getDisruptionData = () => {
$.ajax ({
url: "https://data.edmonton.ca/resource/5yvt-mcye.json",
method:"GET",
dataType: "json",
data: {
"$limit" : 500000,
"$$app_token" : "J33yX1FYA0vwnOA36tGBFLd6l",
},
complete: function(data) {
$serviceDisruptions.empty();
const htmlToAppend =
`<li class="stationGrid">
<div>
ROUTE
</div>
<div>
DESCRIPTION
</div>
<div>
DISRUPTION START
</div>
<div>
EXPECTED END TIME
</div>
</li>`
$serviceDisruptions.append(htmlToAppend);
}
}).then((alerts) => {
setTimeout(app.getDisruptionData, 1000);
alerts.forEach ((disruption) => {
const alertTime = disruption.start_dttm;
const cleanedAlertTime = alertTime.replace("T", " ").replace("/", " ");
const formattedDisplayTime = moment(cleanedAlertTime).format("dddd, MMM D, YYYY, LTS");
const unixDisruptionStartTime = Date.parse(formattedDisplayTime);
const cleanedUnformattedEdmontonTodayTime = unformattedEdmontonTodayTime.replace(",", "");
const unixEdmontonTime = Date.parse(cleanedUnformattedEdmontonTodayTime);
const lastTwentyFour = unixEdmontonTime - unixDisruptionStartTime;
const filteredDisruptions = alerts.filter((alert) => {
if (lastTwentyFour <= 86400 && lastTwentyFour >= 0) {
return true;
} else {
return false
}
})
console.log(filteredDisruptions); => returning blank arrays
});
});
}