Challenge - Creating a function to filter elements using only the flatMap() operator

To show how powerful the flatMap() operator can be we can create a function to emulate the filter() operator using only the flatMap() operator; this function must have the following signature:

filterUsingFlatMap(observable,filter); 

It will receive two parameters and both are mandatory:

  • observable: It is the observable to filter data
  • filter: It is a function that chooses if an element should be filtered or not

And it must return an observable for the filter.

Before coding our function, let's create a test suite to test it.

Let's use mocha as we learned in the previous chapter. We can define our test group using the describe() function:

 var assert = require('assert'); ...

Get Mastering Reactive JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.