Angular RxJS将ObjectA发射合并到ObjectB阵列中

Angular RxJS将ObjectA发射合并到ObjectB阵列中,angular,typescript,rxjs,Angular,Typescript,Rxjs,我有以下从web服务获取数据的代码 @Effect() searchAction$ : Observable<Action> = this._actions$ .ofType(ActionTypes.SEARCH_CASES) .do(val => this._store.dispatch(new SetLoadingAction(true))) .map(action => action.payload) //just interested in

我有以下从web服务获取数据的代码

@Effect()
searchAction$ : Observable<Action> = this._actions$
    .ofType(ActionTypes.SEARCH_CASES)
    .do(val => this._store.dispatch(new SetLoadingAction(true)))
    .map(action => action.payload) //just interested in the payload
    .map(payload => new CaseSearchCriteria(payload)) //form search criteria
    .switchMap(payload => this._httpSearchCases.searchDiseaseCases(payload)
        /*
            1. Return from httpCall is IDiseaseControlCaseManagement[]
            2. Transform this array into SearchDiseaseCaseResults[]
         */
        .do((val) =>{
            console.log('Type of Value: ', typeof val);
            console.log('Is value SearchDiseaseCaseResult? :', val instanceof SearchDiseaseCaseResults);
        })
        .map(res => new LoadSearchResultsAction(res))
    );
但是TS静态分析告诉我这是不正确的

Error:(32, 31) TS2453:The type argument for type parameter 'R' cannot be inferred from the usage. Consider specifying the type arguments explicitly.


Type argument candidate 'SearchDiseaseCaseResults[]' is not a valid type argument because it is not a supertype of candidate 'void'.
因此,我需要一个操作符链(或方法),它允许我

  • 接收ObjectA类型的发射
  • 将ObjectA类型的发射转换为ObjectB类型的发射
  • 接收B类排放物
  • 将ObjectB的每个发射合并到单个数组ObjectB[]
  • 接收ObjectA[]类型的发射
  • 将ObjectA[]类型的发射转换为ObjectB[]类型的数组

  • 除非我误解了你,否则你要找的接线员就是那张普通的老地图

    退一步,让我们关注一下您的切换图

    .switchMap(payload => this._httpSearchCases.searchDiseaseCases(payload)
    
             //    1. Return from httpCall is IDiseaseControlCaseManagement[]
            .map(IDiseaseControlCaseManagementArray => this.mapToSearchDiseaseCaseResultsArray(IDiseaseControlCaseManagementArray) )
    
            .do((val) =>{
                console.log('Type of Value: ', typeof val);
                console.log('Is value SearchDiseaseCaseResult? :', val instanceof SearchDiseaseCaseResults);
            })
            .map(res => new LoadSearchResultsAction(res))
        );
    
    我想这基本上就是你要问的——mapToSearchDiseaseCaseResultArray方法的实现

    而且,如果您可以将单个
    IDiseaseControlCaseManagement
    映射到单个
    SearchDiseaseCaseResult
    ,那么实际上,实现该操作的数组版本非常简单

    因此,假设有一种方法
    convertFrom
    ,可以像这样使用:

    searchDiseaseCaseResult = convertFrom(iDeseaseControlCaseManagement)
    
    然后,用数组

    mapToSearchDiseaseCaseResultArray(sourceArray) {
      return sourceArray.map(caseManagement => this.convertFrom(caseManagement));
    }
    
    当然,您仍然需要编写
    convertFrom
    方法来转换单个项,但一旦编写了,就可以将其插入到上述方法中


    上述内容有意义吗?

    谢谢你,我真的不知道为什么我想不起来(我想我是太努力了,不想使用RxJS)
    mapToSearchDiseaseCaseResultArray(sourceArray) {
      return sourceArray.map(caseManagement => this.convertFrom(caseManagement));
    }