如何在 rxjs 中使用 Observables 实现以下代码?
我在这里想要实现的是我有一个函数数组,每个函数都接受一个对象,修改它并将该对象返回到堆栈中的下一个函数。
function A(res:SomeType){
//Do Something
return res;
}
function B(res:SomeType){
//Do Something
return res;
}
function C(res:SomeType){
//Do Something
return res;
}
let fnPipe = [];
fnPipe.push(A);
fnPipe.push(B);
fnPipe.push(C);
obj= {key:"val"};
fnPipe.forEach((fn)=>{
obj= fn(obj);
});
console.log(obj);
如何在 rxjs 中使用 observables 实现相同的功能?
最佳答案
let fn$ = Observable.from([
x => x + "a",
x => x + "b",
x => x + "c"
])
let value$ = Observable.of("x", "y", "z")
value$
.concatMap(val => fn$.scan((acc, fun) => fun(acc), val))
.subscribe(console.log)
/* prints
"xa"
"xab"
"xabc"
"ya"
"yab"
"yabc"
"za"
"zab"
"zabc" */
关于javascript - 如何使用 rxjs Observables 实现以下功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44626899/