I'm having difficulty understanding your question, but here is some information that may help you find a solution to your problem.
Difference Between Array#filter and RxJS#filter
The filter method in JavaScript arrays is quite similar to the filter method in RxJS Observables (keep in mind that Subjects are also observables).
For instance, consider this predicate that checks if a number is even:
const even = n => n % 2 == 0
These two code snippets demonstrate the similarity:
const arr = [1,2,3,4,5,6,7,8,9];
console.log(arr.filter(even));
Output: [2,4,6,8]
const arr = [1,2,3,4,5,6,7,8,9];
from(arr).pipe(
filter(even)
).subscribe(console.log);
Output:
2
4
6
8
In the first case, Array#filter returns an array where the even
function evaluates to true for each element in the array. In the second version, a stream is created where even
evaluates to true for each element of the stream after filtering.
They are very similar in functionality.
Predicates Explained
A predicate simply returns either true or false, making it easy to identify by its boolean output.
(any) => true // This is a predicate
(any) => false // This is also a predicate
(any) => void // This does not qualify as a predicate.
For example:
(poiData: any) => {
console.log(poiData);
}
is equivalent to (any) => void
, which means it cannot be used for filtering arrays or observables.
Filtering vs Transforming Observables
The basic way to transform an observable is with the map
operator, which alters the elements in the stream without affecting the stream itself (e.g., the number of emissions, their timing, etc.).
If your observable is emitting an array, you can manipulate its elements like this:
accounts.pipe(
map(list => list.filter(element => /* code that returns true or false */))
).subscribe(console.log);
Alternatively, you can filter the entire stream to determine if the list emitted by your BehaviorSubject meets certain criteria:
accounts.pipe(
filter(list => /* code that returns true or false */)
).subscribe(console.log);