Quentin Gillet
Quentin Gillet

Reputation: 69

How to iterate through an API response containing a JSON array in Angular2

This is probably an easy answer, but I already spent days trying to figure it out, without any success.

An HTTP GET returns this body response:

{"members":[{"id":1484,"organization_id":3},{"id":1777,"organization_id":3},{"id":2214,"organization_id":3},{"id":2261,"organization_id":3}]}

I try to iterate trough it in Angular2:

getMembers() {
    this.http.get('http://server/user/members')
    .map(res => res.json())
    .subscribe(
        data => this.members = data.members
    );
}

How can I loop through members and read their property? The html template can do it perfectly well:

<li *ngFor="let m of session.members">
{{m.id}}
</li>

Upvotes: 2

Views: 5079

Answers (2)

Daniel Kucal
Daniel Kucal

Reputation: 9242

If you want to iterate over it in template, try:

<li *ngFor="#m of session.members | async">
   {{m.id}}
</li>

Upvotes: 0

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658037

getMembers() {
    this.http.get('http://server/user/members')
    .map(res => res.json())
    .subscribe(
        data => {
            this.members = data.members;
            this.members.forEach(m => console.log(m.id));
        });
    );
}

Upvotes: 3

Related Questions