2017-12-21 6 views
1

最新の4レコードを表示したいが、すべてのdレコードを表示する。最新の4レコードをangular2に表示したい

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

+0

は '私は= this.moderlist.length-4'操作できますか? – atayenel

+0

no does not working –

答えて

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); 
     }); 
    } 
0

変更forループ内のロジックで、最新の4つのレコードを表示します。

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); 
     } 
    } 
関連する問題