我想在angular2中显示最新的4条记录

时间:2017-12-21 10:43:47

标签: angular multidimensional-array

我想显示最新的4条记录,但它会显示所有d条记录。

public moderlist=[];
public notification1=[];
public createdon1=[];
public notificationlabel:any=[];
public createdonlabel:any=[];

getAllNotifications() {
    debugger;
    this.RedBenchService.getNotifications().subscribe(results => {
        // this.moderlist
        debugger;
        this.moderlist= results;
        for(let i=0; i<this.moderlist.length;i++) {
            this.notification1.push(this.moderlist[i].Notification1);
            this.createdon1.push(this.moderlist[i].CreatedOn);
        }
        this.notificationlabel=this.notification1;   
        this.createdonlabel=this.createdon1;                     
    }, error => {
        console.log(error);
    });
}

我想在angular2中显示最新的4条记录

2 个答案:

答案 0 :(得分:1)

public moderlist=[];
public notification1=[];
public createdon1=[];
public notificationlabel:any=[];
public createdonlabel:any=[];
public fourNotification:any=[];
    getAllNotifications() {
         debugger;
    this.RedBenchService.getNotifications().subscribe(
      results => {
        // this.moderlist
          debugger;
        this.moderlist= results;
        for(let i=0; i<=4;i++)
                {

                   debugger;
                  this.fourNotification.push(this.moderlist[i]);
                  }

              },
      error => {
        console.log(error);
      });
  }

答案 1 :(得分:0)

更改for循环中的逻辑,如下所示。

if(this.moderlist.length >= 4){
     for(let i=this.moderlist.length-1; i>=this.moderlist.length-4;i--) {
            this.notification1.push(this.moderlist[i].Notification1);
            this.createdon1.push(this.moderlist[i].CreatedOn);
        }
     }