Javascript 在rxjs中寻找更干净的扫描方式

Javascript 在rxjs中寻找更干净的扫描方式,javascript,rxjs,Javascript,Rxjs,因此,我看到了一种使用scan的有用方法,即将函数映射到流以更新初始值 const initialState = false; const notThis = (x) => !x; const toggle = clicks.map(()=> notThis) .startWith(initialState) .scan((acc,curr)=> curr(acc)) 但是如果我需要点击的值,我知道我可以写一个部分 const initialState = {klas

因此,我看到了一种使用scan的有用方法,即将函数映射到流以更新初始值

const initialState = false;
const notThis = (x) => !x;
const toggle = clicks.map(()=> notThis)
  .startWith(initialState)
  .scan((acc,curr)=> curr(acc))
但是如果我需要点击的值,我知道我可以写一个部分

const initialState = {klass:'',bool:false};
const toggle = clicks
  .map((y)=> {
    return (x) => {
       let klass  = y.target.className;
       let bool = !x.bool;
       return ({klass, bool});
     };
  })
  .startWith(initialState)
  .scan((acc,curr)=> curr(acc));

如果我合并多个流,这可能非常有用,但它看起来可能过于复杂。有没有更好的方法来完成数据和函数的传递?这是本例的一个bin,您应该问问自己是否需要传递函数。我将代码简化了一点:


或者一个更简单的问题:

谢谢你的反馈,但我发布的这个问题设计得很简单,说明了一个非常复杂的问题,即扫描保持从多个流合并而来的状态。我不知道你的问题,所以我无法帮你,但你的扫描解决方案似乎非常聪明。