liveBook · Manning

Comprehension

As explained in Functional Programming in JavaScript

A concise syntax for producing arrays by combining iteration and filtering expressions.

Array comprehension is concise JavaScript syntax for producing a new array by combining iteration with filtering. It focuses on the desired output rather than loop mechanics. section 3.3

Filter people while selecting their names

const names = [for (p of people) if (p.birthYear === 1903) p.fullname]\n .join(' and ');\n\n// 'John von Neumann and Alonzo Church'\n\nThis combines the filtering performed by filter with the transformation performed by map. section 3.3

[for (p of people) if (p.birthYear ===  1903) p.fullname]
  .join(' and ');

View the complete comprehension example that selects people born in 1903 and joins their names. section 3.3

How Functional Programming in JavaScript covers it

How deep it goes
Defines the syntax and gives a worked example, but does not detail its implementation internals.
Examples
Uses people born in 1903, fullname selection, and joining the resulting names.
What it assumes
Assumes familiarity with JavaScript arrays, for...of iteration, conditions, map, and filter.
What you can do after
Apply array-comprehension syntax to combine filtering and mapping in a concise JavaScript expression.
How it connects
Connects array comprehension with map, filter, higher-order functions, and functional programming.

Also explained in

Compare how 6 books explain comprehension →

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.