Angular2过滤对象数组

分享于2023年03月15日 angular reactive-programming rxjs rxjs5 typescript 问答
【问题标题】:Angular2 filter array of objectsAngular2过滤对象数组
【发布时间】:2023-03-14 12:56:02
【问题描述】:

我将从添加代码开始,我得到的结果,最后我想得到什么,如果可能的话。

我得到的结果是 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 =  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]
        });
    ....
    }
}

过滤器结构

我正处于 angular2 和响应式编程的早期学习阶段,我想要进行过滤,以便映射结果仅是具有至少 1 个项目的数组。

谢谢

  • .filter(each => ... 中的数据结构是什么?
  • 我已经上传了过滤器的结构,谢谢

【解决方案1】:

.map 代替 .filter

.map(each => {
    return each.filter(array => array.length > 0)
}

【讨论】: