简体   繁体   English

如何使用 rxjs 的过滤器运算符?

[英]How can I use filter operator of rxjs?

I have some code, where I have observable.我有一些代码,我可以观察到。 I want to change array filter method to filter of rxjs.我想将数组过滤方法更改为过滤 rxjs。 How can I use filter operator of rxjs?如何使用 rxjs 的过滤器运算符?


@Input() public articleTags: Observable<ALArticleTag[]>;
public selectedArticleTags: ALArticleTag[] = [];
public ngAfterViewInit(): void {
        this.articleTags.pipe(
            take(1),
            map(tags =>  {
                return this.selectedArticleTags = this.article.tagIds ? tags.filter(tag => {
                    return this.article.tagIds.includes(tag.id);
                }) : [];
            }),
        ).subscribe(response => {
            this.selectedArticleTags = response;
            this.changeDetection.markForCheck();
        });
    }

Do take note that RxJS's filter operator is quite different from JavaScript's native Array.filter() method.请注意,RxJS 的过滤器运算符与 JavaScript 的原生Array.filter()方法完全不同。

RxJS's filter operator allows you to RxJS 的过滤器操作符允许你

Filter items emitted by the source Observable by only emitting those that satisfy a specified predicate.通过仅发出满足指定谓词的项来过滤源 Observable 发出的项。

In other words, the RxJS filter operator excludes observable from the stream that meets a certain condition, which is quite different from what Array.filter() does, which is to filter/remove objects or values from the array based on a certain condition.换句话说,RxJS filter算子从 stream 中排除了满足一定条件的 observable,这与Array.filter()所做的完全不同,后者是根据特定条件从数组中过滤/删除对象或值。

For instance, the below sequence uses the RxJS filter() operator to filter out tags whose length are less than 1.例如,下面的序列使用 RxJS filter()运算符过滤掉长度小于 1 的tags

this.articleTags
  .pipe(
    filter(tags => tags.length > 1),
  ).subscribe(res => {
    console.log(res);
    // do the rest
  })

Therefore, the resulting output when the observable values have been returned would be articleTags whose length is more than 1.因此,当返回可观察值时,生成的 output 将是长度大于 1 的articleTags

Hence, I don't think you should use the RxJS filter operator to replace the filtering operation within the map() operator.因此,我认为您不应该使用 RxJS filter运算符来替换map()运算符中的过滤操作。

From what I see, you want to filter tags by if this condition is satisfied this.selectedArticleTags = this.article.tagIds and then you filter the array by this condition tags.filter(tag => this.article.tagIds.includes(tag.id)) .据我所知,如果满足此条件,您想过滤标签this.selectedArticleTags = this.article.tagIds ,然后按此条件过滤数组tags.filter(tag => this.article.tagIds.includes(tag .id)) .

The code should look like this:代码应如下所示:

@Input() public articleTags: Observable<ALArticleTag[]>;
public selectedArticleTags: ALArticleTag[] = [];
public ngAfterViewInit(): void {
        this.articleTags.pipe(
            take(1),
            filter(tags => this.selectedArticleTags = this.article.tagIds),
            map(tags => tags.filter(tag => this.article.tagIds.includes(tag.id));
            }),
        ).subscribe(response => {
            this.selectedArticleTags = response;
            this.changeDetection.markForCheck();
        });
    }

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

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