Javascript RxJS5:.combineLatest()与投影函数?

Javascript RxJS5:.combineLatest()与投影函数?,javascript,typescript,rxjs,rxjs5,reactivex,Javascript,Typescript,Rxjs,Rxjs5,Reactivex,我需要一种方法将值从一个可观测值传递到两个函数,每个函数接受一个值,然后返回一个可观测值(只发送一个值,然后完成)。我希望.combinelatetest()能允许我传递一个投影函数,但它不能 示例代码(不工作): const ac=[1,2,3];//仅作为示例,我的数组中有复杂类型,而不是数字 可观察。从(ac) .联合测试( //processFn应该获取一个数字作为参数并返回Observable //无法工作,因为.CombineTest不接受两个函数作为参数:( n=>processF

我需要一种方法将值从一个可观测值传递到两个函数,每个函数接受一个值,然后返回一个可观测值(只发送一个值,然后完成)。我希望
.combinelatetest()
能允许我传递一个投影函数,但它不能

示例代码(不工作):

const ac=[1,2,3];//仅作为示例,我的数组中有复杂类型,而不是数字
可观察。从(ac)
.联合测试(
//processFn应该获取一个数字作为参数并返回Observable
//无法工作,因为.CombineTest不接受两个函数作为参数:(
n=>processFn1(n),
n=>processFn2(n)
)
.map(([result1,result2]=>{
//result1,result2应该是平坦的数字,而不是可观察的
})
);

有什么办法吗?

您使用CombineTest运算符的想法是正确的,但它位于错误的位置。如果您需要展开观察对象,您应该使用mergeMap。这是一个JSBIN,可以实现您的预期:

代码如下所示:

const ac = [1, 2, 3];

Rx.Observable.from(ac)
  // use mergeMap, it takes a function that accepts a value and
  // returns an observable. MergeMap will listen to this observable
  // under the hood and next the result of this observable down the
  // the chain
  .mergeMap(val => {
    // Here we return an observable that combines the result of the
    // call to both the functions
    return Rx.Observable.combineLatest(fake(val), fake2(val));
  })
  .subscribe(val => console.log(val));

RxJS通常都是这样,但需要一些时间才能习惯:)
const ac = [1, 2, 3];

Rx.Observable.from(ac)
  // use mergeMap, it takes a function that accepts a value and
  // returns an observable. MergeMap will listen to this observable
  // under the hood and next the result of this observable down the
  // the chain
  .mergeMap(val => {
    // Here we return an observable that combines the result of the
    // call to both the functions
    return Rx.Observable.combineLatest(fake(val), fake2(val));
  })
  .subscribe(val => console.log(val));