我有一个数据结构,通过redux选择器我有studentIds = [123,124] 我还在我的redux商店中有学生实体对象,其中包含123和124的对象详细信息 我想订阅[学生实体123,学生实体124]的数组。
我试过以下。但我有两个问题 1.减少后,不会打印任何值 2.我需要在步骤-2之后重置减少
this.store
.select(FilterSelector.getSelectedStudents)
// Step - 1 Here we get an observable stream which has value of [123, 124]
.switchMap(studentIds => {
return from(studentIds);
})
// Step - 2 Here student-id 123 and 124 are streamed independently. the purpose is I can lookup student ids independently for the next step
.mergeMap(studentId => {
return this.store.select(StudentSelector.getStudentById(studentId));
})
// Step - 3 Here the student-id = 123 object and student-id = 124 object are streamed independently
.pipe(
reduce((acc, curr: Student) => {
acc.push(curr);
return acc;
}, [])
// Step - 4 Here the student-id = 123 object and student-id = 124 object are combined together and streamed as 1 single array
)
.subscribe(d => console.log(d));
// I am expecting the array of student objects
答案 0 :(得分:0)
您可以使用其他策略将两个实体放在一起
this.store.select(FilterSelector.getSelectedStudents)
.flatMap((ids:Array<number>)=>merge(ids.map(studentId=>this.store.select(StudentSelector.getStudentById(studentId)))))
.subscribe((data)=>{...})