lodash _.filter


Example

Filtering a list down to only the elements we want to do. Lodash provides a function called _.filter filters elements based on the predicate function you provide. A predicate is a function that takes data in and returns either true or false.

Let's look at how we'd get just the even numbers from a list of the numbers 0 through 9:

var numbers = _.range(0,10);      // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

var evenNumbers = _.filter(numbers, function(e){ return e % 2 == 0; });
// evenNumbers is now [ 0, 2, 4, 6, 8 ]