如何使用我的数组中的旧日期保留对象

Spi*_*lot 5 javascript arrays comparison loops date

我循环遍历所有具有日期属性的对象数组.在循环中设置的条件我将对象的日期与今天的日期进行比较应该只处理我想要保留的数组中的一些对象,因为旧的日期,但是,这个条件是删除数组中的所有对象某些原因.

它不能使用getTime().getTime从数组中删除所有内容.就像我在这里尝试的:

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
Run Code Online (Sandbox Code Playgroud)

更新 我的解决方案:

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 
Run Code Online (Sandbox Code Playgroud)

Con*_*Fan 1

如果,如 RobG 在评论中提到的, 的值listOfEventsObject[i].date是一个字符串,您可以先解析它,并将生成的 Date 值与 进行比较Date.now()

if (Date.parse(listOfEventsObject[i].date) < Date.now()) { 
    ... 
}
Run Code Online (Sandbox Code Playgroud)