Rx Js Filter Method Does Not Return The Filter Array
Solution 1:
Because this is not how you're supposed to use Observables in RxJS. Filters in RxJS always return another Observables and never directly values the processed.
Operator filter()
expects you to return boolean true
or false
based on whether you want to to include or exclude the item from the processed Observable stream. Since you're passing the entire array as a single value with Rx.Observable.of(items)
it always passes everything or nothing (the returned array is converted into boolean).
So let's use Observable.from
static method to create an Observable from an iterable that'll emit each item in the array as a single value and then just filter()
to exclude all items that don't match the predicate.
const filterBy = {title: 'javascript'};
const items = [{title: 'javascript'}, {title: 'css'}, {title: 'html'}];
const observable = Rx.Observable.from(items).filter(item => {
return item.title.indexOf(filterBy.title) !== -1;
});
observable.subscribe(f =>console.log(f));
Solution 2:
You can return an array with the toArray opereator:
const filterBy = {title: 'javascript'};
const items = [{title: 'javascript'}, {title: 'css'}, {title: 'html'}];
const observable = Rx.Observable.from(items).filter(item => {
return item.title.indexOf(filterBy.title) !== -1;
}).toArray();
Post a Comment for "Rx Js Filter Method Does Not Return The Filter Array"