O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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'); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required