2

I'd like to filter an observable, but am getting unexpected results.

I am using the following import statements:

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';

This example returns nothing:

Observable.of([0, 1, 2, 3, 4, 5])
.filter((x: any) => x % 2 === 0)
.subscribe(data => console.log(data));

This example returns the entire result set [0,1,2,3,4,5]:

Observable.of([0, 1, 2, 3, 4, 5])
.filter((x: number[], idx) => x[idx] % 2 === 0)
.subscribe(data => console.log(data));

Does anyone see syntax errors and/or something I am missing? Thank you!

1 Answer 1

2

You need to use from if you want to pass values as iterable (array in current case)

import 'rxjs/add/observable/from';

Observable.from([0, 1, 2, 3, 4, 5])
.filter((x: any) => x % 2 === 0)
.subscribe(data => console.log(data));

Or if you want stick with of pass values as arguments

Observable.of(0, 1, 2, 3, 4, 5)
.filter((x: any) => x % 2 === 0)
.subscribe(data => console.log(data));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.