简体   繁体   English

RXJS 更好的方法来 map 和过滤器数组内部的 Observable<object><div id="text_translate"><p> 这是我想要完成的一个非常简单的版本。 目前它正在按预期工作,但我只是想知道是否有更短/更清洁的方法来实现 pipe() 内部的运算符</p><p><strong>当前行为:</strong>在包含数组“项目”的属性的可观察对象上调用 pipe。 在 pipe 内部,过滤、排序然后将项目发布到行为主体。</p><pre> public items$: BehaviorSubject public testObservable = () =&gt; of({ Test: '123', Properties: 'props', Items: [ { key: 1, val: 'test1' }, { key: 2, val: 'test2' }, { key: 3, val: 'test3' } ] }); testMethod() { this.testObservable().pipe( pluck('Items'), map(items =&gt; items.filter(item =&gt; item.key,= 2)). map(items =&gt; items,sort((a. b) =&gt; (a.key &gt; b?key: 1, -1))). tap(items =&gt; { this.items$;next(items) }) );</pre></div></object>

[英]RXJS better way to map and filter array inside of Observable<object>

This is a very simple version of what I am trying to accomplish.这是我想要完成的一个非常简单的版本。 Currently it is working as intended but I was just wondering if there a shorter/cleaner way to implement the operators inside of pipe()目前它正在按预期工作,但我只是想知道是否有更短/更清洁的方法来实现 pipe() 内部的运算符

Current Behavior: call pipe on an observable that contains a property with array 'Items'.当前行为:在包含数组“项目”的属性的可观察对象上调用 pipe。 Inside of the pipe, filter, sort and then publish the items to a behavior subject.在 pipe 内部,过滤、排序然后将项目发布到行为主体。

  public items$: BehaviorSubject

  public testObservable = () =>
    of({
      Test: '123',
      Properties: 'props',
      Items: [
        { key: 1, val: 'test1' },
        { key: 2, val: 'test2' },
        { key: 3, val: 'test3' }
      ]
    });

  testMethod() {
    this.testObservable()
      .pipe(
        pluck('Items'),
        map(items => items.filter(item => item.key != 2)),
        map(items => items.sort((a, b) => (a.key > b.key ? 1 : -1))),
        tap(items => { this.items$.next(items) })
      );

Well I you could write just好吧,我可以只写

this.testObservable()
  .pipe(
    map(value => 
        value.items.filter(item => item.key != 2)
             .sort((a, b) => (a.key > b.key ? 1 : -1))
    ),
    tap(this.items$.next)
  )

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

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