Angular2过滤器对象阵列
问题描述:
我将开始添加代码,得到的结果以及最终我想要获得的结果,以及是否有可能。Angular2过滤器对象阵列
其中我得到的结果是数组[对象,对象,...],其中对象是阵列
export class SomeService {
....
....
public someFunction(): MyObject[]{
Observable
.forkJoin(this.userItemsA(userId), this.userItemsB(userId), etc)
.filter(each => {
for (let array of each) {
let x: any = <any> array;
return x.length > 0;
}
})
.map(result => {
return result;
})
.subscribe(result => {
/// what i would like to do for example assuming only 1st array has items
/// do something here with result[0]
/// return MyObject[] from result[0]
});
....
}
}
滤波器结构
I” m在angular2和反应式编程的早期学习阶段,我想要过滤以便映射结果将只有至少有一个项目的数组。
谢谢
答
代替.filter
使用.map
.map(each => {
return each.filter(array => array.length > 0)
}
答
不幸的是,这并不与forkJoin
工作。它的作用是将多个Observable连接成单个 Observable,因此如果它们中的任何一个被过滤掉,则整个连接的Observable被中断/过滤。
正如@Martin所述,您必须在map
分支中进行筛选。
你在'.filter(each => ...'中的数据结构如何? – martin
我已经上传了过滤器的结构,thx – Remus