How to save objects with old date from my array

I iterate over an array of objects, all have a date property. The conditional value that I set inside the loop to compare the date of the object with today's date should only take care of a few objects in the array that I want to keep because of the old date, however this condition deletes all the objects in the array for some reason.

The getTime () function does not work. getTime removes everything from the array. As I tried here:

constructor ( public navCtrl: NavController, public modalCtrl: ModalController, public loading: LoadingController, public popoverCtrl: PopoverController, public getPostSrvc: getPostsService) { this.listOfEvents = []; let that = this; function getPostsSuccess (listOfEventsObject) { for (var i in listOfEventsObject) { if(listOfEventsObject[i].date.getTime() < Date.now()){ that.listOfEvents.push(listOfEventsObject[i]); }//close if }//close for loop }//close function }//close constructor 

UPDATE My solution:

 export class Home { listOfEvents: Array<any> = []; parseDate: number; today : number; constructor ( //constructor stuff ){ for (var i in listOfEventsObject) { that.today = Date.now(); that.parseDate = Date.parse(listOfEventsObject[i].date); if( that.parseDate > that.today){ that.listOfEvents.push(listOfEventsObject[i]); }//close if }//close for }//close constructor }//close export 
+5
source share
2 answers

If, as RobG mentioned in the comment, the value of listOfEventsObject[i].date is a string, you can parse it first and compare the resulting Date with Date.now() :

 if (Date.parse(listOfEventsObject[i].date) < Date.now()) { ... } 
+1
source

I can’t say for sure, because I don’t know what listOfEventsObject[i].date is. But if this data contains timestamps, you can convert it to milliseconds and compare it with Date.now() .

if (listOfEventsObject[i].date.getTime() < Date.now())

let me know if this is not the answer you are looking for, greetings.

0
source

Source: https://habr.com/ru/post/1269052/


All Articles