繁体   English   中英

RxJS筛选器/搜索主题,可观察对象或BehaviorSubject

[英]RxJS Filter / Search Subject, Observable, or BehaviorSubject

我刚刚开始学习RxJS。 我没有多大运气就试图做的一件事是,弄清楚如何搜索/过滤主题,或者创建一个可以观察的数组。

我尝试过管道传递和过滤Subject和BehaviorSubject,但是谓词中的值是RxJS特定的。 根据我在各种文章上所读的内容,方法是观察数组以使用Subject。

我可以轻松地拥有两个变量,一个数组和主题,然后搜索该数组。 但我想完成这一变量。

在淘汰赛中,可以搜索观察到的数组。

RxJS有可能吗?

提前致谢。

例:

layers: Rx.Subject<any> = new Rx.Subject<any>();

toggleLayer (layerId, visible) {

  //find layer we need to toggle

 // How do I search the subject to get the values added in next()?

   // tried from(this.layers), pipe does not fire
    const source = of(this.layers);

    const example = source.pipe(filter((val, index) => {
   //val is subject, and only iterates once, even if more than one value in subject
      // tslint:disable-next-line:no-debugger
      debugger;
      return false;
    }));

    const sub = example.subscribe((val) => {
      // tslint:disable-next-line:no-debugger
      debugger;
    });

}

private addLayer = (layerName, layerObj, layerType) => {

  // component class is subscribed to layers subject. Update UI when layer is added
  this.layers.next({
      layerId: this.layerId,
      name: `${layerName}_${this.layerId}`,
      layerObj: layerObj,
      visible: true,
      layerType: layerType
    });

}

我不清楚您要询问的内容的100%,但是也许这个例子可以为您提供帮助。

const filterSubject = new BehaviorSubject<string>('b');
const dataSubject = new BehaviorSubject<string[]>(['foo', 'bar', 'baz', 'bat']);
const dataObservable = combineLatest(filterSubject, dataSubject).pipe(
  // given an array of values in the order the observables were presented
  map(([filterVal, data]) => data.filter(d => d.indexOf(filterVal) >= 0))
);

dataObservable.subscribe(arr => console.log(arr.join(',')));
// bar, baz, bat

使用combineLatest ,只要过滤器值或数据数组发生更改,就可以更新dataObservable值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM