Iterates over a collection (Array, Object, Map, Set) and returns a new collection of the same type containing only the values for which the predicate function evaluates to true.
Like many Collection methods in Conductor, filter works with both asynchronous & synchronous mappers. If you use a synchronous predicate function, filter will work like Array.prototype.filter and return a Collection synchronously.
constvalues= [0,1,2,3]constisEven= x => x %2===0filter(isEven, values) // [0, 2]
If you use an asynchronous mapper, filter will return a Promise, and you will need to use await or Promise.prototype.then to retrieve the new collection.
constvalues= [0,1,2,3]constisEven=async x => x %2===0filter(double, values) // Promise<Pending>awaitfilter(double, values) // [0, 2]
If you use an asynchronous predicate function, all calls to the predicate function will be done in parallel, but the input collection's order will be preserved.
examples
basic example
import { filter } from'conductor'constvalues= [0,1,2,3]constisEven= x => x %2===0filter(isEven, values) // [0, 2]
Here, we are just using filtering on an array to keep only even values. The predicate function is synchronous.
other data structures
using the index in the predicate function
using an asynchronous predicate
If your predicate function is asynchronous, you will need to use await (or Promise.prototype.then) because the result will be a Promise.