2016-08-31 76 views
5

我有一個名爲comments的對象數組,我試圖只選擇一個具有我需要的郵件的ID給另一個對象數組。問題是我無法找到複製我找到的對象的方法。這是我的功能:Angular2 foreach對象?

comments = []; 
commentspart = []; 
private loadPartComments(id){   
      this.comments.forEach(element => { 
      if (element.postId == id) { 
       this.commentspart = ????; 
       } 
      }); 
      return this.commentspart; 
     } 

謝謝。

+0

怎麼樣:'this.commentspart = element;'? –

回答

7

我猜你正在尋找filter

comments = []; 
commentspart = []; 
private loadPartComments(id){   
      this.commentspart = this.comments.filter(element => { 
       return element.postId == id; 
      }); 
     } 

它會給你的基於ID的意見過濾陣列。

希望這有助於!