liveBook · Manning

Filtering

As explained in Functional Programming in JavaScript

A function exposed by Lodash as part of its utility API.

Filtering selects the elements that satisfy a condition and returns them as a smaller subset of the original collection. The condition is a predicate function. section 3.3

Select valid people

An imperative loop checks each person and pushes valid values into a result array. Lodash expresses the same operation directly: `_(persons).filter(isValid).map(fullname);`. The filter keeps only values for which `isValid` returns true, while map transforms the survivors. section 3.3

Filter, transform, and combine

To find people born in 1903 and format their names, define `const bornIn1903 = person => person.birthYear === 1903;` and write `_(persons).filter(bornIn1903).map(fullname).join(' and ');`. The result is `John von Neumann and Alonzo Church`. section 3.3

Refactor sequential data cleanup

A loop that removes null values, normalizes names, removes duplicates, and sorts them becomes `_.chain(names).filter(isValid).map(s => s.replace(/_/, ' ')).uniq().map(_.startCase).sort().value();`. Each operation states one transformation in the data flow. section 3.4

const p1 = new Person('Haskell', 'Curry', '111-11-1111');
p1.address = new Address('US');
p1.birthYear = 1900;

const p2 = new Person('Barkley', 'Rosser', '222-22-2222');
p2.address = new Address('Greece');
p2.birthYear = 1907;

const p3 = new Person('John', 'von Neumann', '333-33-3333');
p3.address = new Address('Hungary');
p3.birthYear = 1903;

const p4 = new Person('Alonzo', 'Church', '444-44-4444');
p4.address = new Address('US');
p4.birthYear = 1903;

Use the person records to try predicates based on country, birth year, or missing values before chaining another transformation. section 3.3

How Functional Programming in JavaScript covers it

How deep it goes
Defines filtering, demonstrates predicates and chains, and shows a loop implementation without developing deeper algorithmic internals.
Examples
Demonstrates filtering valid people, selecting people born in 1903, and cleaning names through a Lodash chain.
What it assumes
Assumes basic JavaScript functions and arrays while building on predicates, lambdas, and higher-order functions.
What you can do after
Apply it via Lodash to replace manual selection loops with readable predicate-based data pipelines.
How it connects
Connects filtering with predicates, map, reduce, higher-order functions, Lodash, and declarative function chains.

Also explained in

Compare how 33 books explain filtering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.