简体   繁体   English

在Angular App中使用Observables和过滤数组列表

[英]Using Observables and Filtering an Array List in Angular App

I am using observables in my Angular app, and it's working as expected. 我在Angular应用程序中使用了可观察对象,并且按预期方式工作。 However, I am running into one situation that's a little confusing. 但是,我遇到一种令人困惑的情况。 In one component I am successfully subscribing to the observable, and then filtering the results, like this: 在一个组件中,我成功订阅了可观察对象,然后过滤结果,如下所示:

this.clientsService.getAll()
    .subscribe(resRecordsData => {
        this.records = resRecordsData;
        this.inactiveRecords = this.records.filter(record => record.category && record.category.includes('inactive'));
        this.records = this.inactiveRecords;
    },
    responseRecordsError => this.errorMsg = responseRecordsError);

However, in another component, while I am doing virtually the same thing, I am getting an error printed to the console that reads: 但是,在另一个组件中,尽管我实际上在做同样的事情,但是在控制台上却显示一条错误消息,内容为:

EXCEPTION: _this.records.filter is not a function 例外:_this.records.filter不是函数

This what that component looks like: 该组件看起来像这样:

optionReceived(option) {
    console.log('Consulting: ' + option.toolbarLabel);
    if (option.toolbarLabel === 'OT') {
        console.log('Consulting: OT Filter Activated!');
        this.clientService.getByCategory('consulting', 1, this.pagesize)
        .subscribe(resRecordsData => {
            this.records = resRecordsData;
            this.OTRecords = this.records.filter(record => record.type && record.type.includes('OT'));
            this.records = this.OTRecords;
        },
        responseRecordsError => this.errorMsg = responseRecordsError);
    } else {
        return this.records;
}

What's the issue in the second case? 第二种情况是什么问题? Why am I getting the error there and not in the first case? 为什么我在那里出现错误,而不是第一种情况?

Why not filter before you subscribe 为什么不过滤,然后再订阅

this.clientsService.getAll()
    .filter(record => record.category && record.category.includes('inactive'))
    .subscribe(
        resRecordsData => {
            this.records = resRecordsData;
        },
        err => this.errorMsg = err
    );

I believe Angular uses RxJS, take reference from http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-filter 我相信Angular使用RxJS,请参考http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-filter

It is because of the async nature, you can use skipWhile operator to 由于异步的性质,您可以使用skipWhile运算符来

this.clientService.getByCategory('consulting', 1, this.pagesize)
    .skipWhile(data => {
         if(data.length) {
             return false;
         }
     })
    .subscribe(resRecordsData => {
        this.records = resRecordsData;
        this.OTRecords = this.records.filter(record => record.type && record.type.includes('OT'));

    },
    ((responseRecordsError) => {this.errorMsg = responseRecordsError});

Note : Make sure that this.records=[]; 注意:确保this.records=[]; is instantiated 被实例化

Update : You should not assign this.records=this.OTRecords; 更新:您不应分配this.records=this.OTRecords; inside the subscription because of which you got the undefined error. 在订阅中,因此会出现不确定的错误。

In the first case filtered elements might contain more than one object(array) but not in the second case 在第一种情况下,过滤后的元素可能包含多个对象(数组),但在第二种情况下不包含

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

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