I have an array of userIds. For example: ['jd', 'abc']
. And I'm looping through the userIds to fetch for full names through api. In the end, I want to turn above array into [ {userId: 'jd', name: 'John Doe"}, {userId: 'abc', name: 'full name'}]
.
code below: I tried returning arrEnriched
inside subscribe and also tried returning outside the forkjoinm. Both didn't work as expected.
getUserObj(arr){
const arrEnriched = [];
forkJoin(arr.map((item)=> {
return this.userService.getUserName(item).pipe(pluck('name'));
})).subscribe( (arrayOfHits)=>{
const names = arrayOfHits.map(...); // transform data
arrEnriched = names.map(...); // map from full name to object
console.log(arrEnriched); // as expected here
//return arrEnriched;
});
// return arrEnriched;
}
And then I'm trying to use this function in my ngOnInit()
ngOnInit(){
this.childOneComponent.users = this.getUserObj(this.arrUsers);
this.childTwoComponent.users = this.getUserObj(this.anotherArrUsers);
}
Thanks.