I have two api calls, one for getting all categories and another to get the posts. In posts api, I'm getting the categories ids, I just need to call the categories and get the categories name and add the name instead of ids in the post calls. I know it's not recommended to use nested api calls, so I used flatMap
but have no idea how to implement it.
categories
[
{
"id": 5,
"name": "Angular",
"slug": "angular",
"parent": 4
},
{
"id": 4,
"name": "Tutorial",
"slug": "tutorial",
"parent": 0
}
]
posts
[
{
"id": 1,
"categories": [
5,
4
]
},
{
"id": 2,
"categories": [
5
]
}
]
code
this.blogService.getCategories()
.pipe(
flatMap((categories: BlogCategoryModel[]) => {
console.log(JSON.stringify(categories)); // I think I have to do logic here
return this.blogService.getPosts();
})
).subscribe(posts => console.log(JSON.stringify(posts)));
expected result
[
{
"id": 1,
"categories": [
"Angular",
"Tutorial"
]
},
{
"id": 2,
"categories": [
"Angular"
]
}
]