Angular2过滤对象数组
Angular2 filter array of objects
我将从添加代码开始,得到我得到的结果,最后我想得到什么,如果可能的话。
我得到的结果是 Array[object, object, ...] 其中 object 是 Array
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]
});
....
}
}
Filter structure
我正处于 angular2 和响应式编程的早期学习阶段,我想要进行过滤,以便地图结果仅包含至少包含 1 个项目的数组。
谢谢
而不是 .filter
使用 .map
.map(each => {
return each.filter(array => array.length > 0)
}
不幸的是,这不适用于 forkJoin
。它的作用是将几个 Observable 加入 单个 Observable,因此如果其中任何一个被过滤掉,整个加入的 Observable 将得到 interrupted/filtered.
如@Martin 所述,您必须在 map
分支中进行过滤。
我将从添加代码开始,得到我得到的结果,最后我想得到什么,如果可能的话。
我得到的结果是 Array[object, object, ...] 其中 object 是 Array
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]
});
....
}
}
Filter structure
我正处于 angular2 和响应式编程的早期学习阶段,我想要进行过滤,以便地图结果仅包含至少包含 1 个项目的数组。
谢谢
而不是 .filter
使用 .map
.map(each => {
return each.filter(array => array.length > 0)
}
不幸的是,这不适用于 forkJoin
。它的作用是将几个 Observable 加入 单个 Observable,因此如果其中任何一个被过滤掉,整个加入的 Observable 将得到 interrupted/filtered.
如@Martin 所述,您必须在 map
分支中进行过滤。