I have an object that looks like this:
myObject = {
publishedDate: string;
Url: string;
Title: string;
}
The property "publishedDate" is a string in the format "19/01/2021". As the example, it can have a date in the future, i.e. a value greater than today. In other words I want to keep all objects where publishedDate is equal or less than today (and delete all that is over). The objects is saved in my state (array):
this.state.data
I first make a date variable for todays value to compare it with publishedDate. I also use moment() to format it in the same date format as the object:
let today = moment().format("DD/MM/YYYY");
I then use a filter method to compare and filter out the data:
this.state.data.filter(item => new Date(item.publishedDate) >= new Date(today))
And I have also tried:
this.state.data.filter(function(item) {
if (new Date(item.publishedDate) >= new Date(today)) {
return false;
}
return true;
});
Not only it does not work, I still have all the objects in the array and nothing is filtered out. What is wrong with the code?