I am trying to dynamically build an array for linkObjects. Dynamically because I need to read the Ngrx Store and get parameters. like in the heatmap
public linksObject = [
{
name: 'Consumption',
link: ['../consumption'],
},
{
name: 'Heatmap',
link: ['../heatmap&res=foo'],
}
];
I have a switchMap that builds out the individual objects, but I need to append it to this.linkObjects
the second switchMap is getting an array of roles like ['admin', 'test', 'field_engineer'] and inside getLinkObjects it is building the object:
{name: label, link:['..\ ...']}
pages is just a static list of labels ['consumption', 'heatmap']
public getLinkObjects(roles: string[]): Observable<any> {
return from(this.pages).pipe(
switchMap(
(page): Observable<ILinksObject> =>
from(roles).pipe(
map((role) => ({
name: page,
link: [`../${page}&role=${role}`],
})),
),
),
);
}
this.fetchRoles()
.pipe(
switchMap((roles: IUserSite[]) => this.getCurrentRoles(roles)),
switchMap((role: string[]) => this.getLinkObjects(role)),
// concat the objects emitted by switchMap
)
.subscribe();
I tried the following 3 ways, that didn't work:
- adding a push in the subscribe
- adding a tap
this.fetchRoles()
.pipe(
switchMap((roles: IUserSite[]) => this.getCurrentRoles(roles)),
switchMap((role: string[]) => this.getLinkObjects(role)),
// concat the objects emitted by switchMap
)
.subscribe(this.linkObjects.push);
this.fetchRoles()
.pipe(
switchMap((roles: IUserSite[]) => this.getCurrentRoles(roles)),
switchMap((role: string[]) => this.getLinkObjects(role)),
tap((role) => this.linkObjects.push(role))
// concat the objects emitted by switchMap
)
.subscribe();
this.fetchRoles()
.pipe(
switchMap((roles: IUserSite[]) => this.getCurrentRoles(roles)),
switchMap((role: string[]) => this.getLinkObjects(role)),
tap(console.log), // this gets printed
reduce((acc, role) => acc.concat(role), this.linksObject),
tap(console.log), // this does not get printed
)
.subscribe(console.log);
I need help to point me in the direction of the correct rxjs operator to use and help troubleshooting the reason as to why those 2 solutions didn't work.