Javascript 角度7-向数组中添加数据而不是替换数据

Javascript 角度7-向数组中添加数据而不是替换数据,javascript,arrays,get,angular7,Javascript,Arrays,Get,Angular7,点击按钮后,我从wordpress api获取数据: <a (click)="initGetComments()">Get comments</a> 我希望将所有这些请求数据存储到一个数组中,而不是替换它。如果在前一个循环完成后发送由for循环引发的每个请求,那就太好了。也许这会帮助您: this.commentsResults.merge(数据)。 而不是:this.commentsResults=data只需使用this.commentsResults.push(

点击按钮后,我从wordpress api获取数据:

<a (click)="initGetComments()">Get comments</a>


我希望将所有这些请求数据存储到一个数组中,而不是替换它。如果在前一个循环完成后发送由for循环引发的每个请求,那就太好了。

也许这会帮助您:

this.commentsResults.merge(数据)
而不是:
this.commentsResults=data

只需使用
this.commentsResults.push(数据)

感谢您的目标定位,它创建了数组而不是记录数组,但我创建了另一个空数组,我在其中推单记录。哦,我错过了数据将是数组的信息。:)
export class AppComponent {

  commentsResults: CommentsItem[] = [];

  getComments(ID:string): Observable<CommentsItem[]> {
    console.log(ID);

    return this.http
      .get(`https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/${ID}/replies/?number=1`)
      .map(res =>
        res.comments.map(
          item =>
            new CommentsItem(
              item.post.ID,
              item.ID,
              item.author.name,
              item.author.avatar_URL,
              item.date,
              item.raw_content
            )
        )
      );
  }

  initGetComments(){
    var posts = document.getElementsByClassName('single-post');

    var i=0;
    for( i; i < posts.length; i++ ) {
      this.getComments(posts[i].id).subscribe(data => {
        this.commentsResults = data;
        console.warn("commentsResults =", this.commentsResults);
      })
    }

  }
}

export class CommentsItem {
  constructor(
    public post_ID: number,
    public ID: number,
    public author: string,
    public avatar: string,
    public date: string,
    public content: string
  ) {}
}