Reputation: 299
How gets all users from their user id and iterate it, and when i click a specific user i get all his post and comments?
you can fetch the posts from this API: https://jsonplaceholder.typicode.com/posts
and their comments from this API: https://jsonplaceholder.typicode.com/comments
Here is a my test stackblitz project: https://stackblitz.com/edit/angular-g5fqzi
getUserPosts(userId: number) {
this.http.get(`${this._postsURL}`)
//.pipe(filter(data => userId === userId))
//this.http.get(`${this._postsURL}/${userId}`)
.subscribe(data => {
//this.UserPosts = data;
let resources = data[userId];
this.UserPosts = resources;
console.log(this.UserPosts);
})
Upvotes: 0
Views: 2298
Reputation: 560
Qusay, if I understood right you are trying to get only the posts of a specific userId.
If you are making a new HTTP request you could pass the userId in the request get only the posts you want like this:
this.http.get(`${this._postsURL}?userId=${userId}`)
.subscribe(data => {
// do stuff
});
But if you have already all posts in memory (from a previous request) and just want to select the ones of a specific userId you could do this:
const userPosts = this._postsArray.filter(post => post.userId == userId);
or if there is always only one result, you can use the method find
instead of filter
, just keep in mind that find returns the value and filter returns a new array with the value.
const userPosts = this._postsArray.find(post => post.userId == userId);
I hope this helps.
Upvotes: 1