lecture 11 of 150 completed

Array methods and the empty list

map, filter, find, some and every replace most loops. On an empty list they give answers that let a test pass while checking nothing.

What you'll learn

  • What map, filter, find, some and every each return
  • What each one returns for an empty list
  • How to write a check that fails when there are no results

A Shelf reader filters the catalogue to books that are available. A test checks that every result is available. The filter breaks and returns no books. The test passes.

const results = [];

const allAvailable = results.every((book) => book.available);
console.log(allAvailable);
true

every takes a callback. It calls the callback with each item and gives true when the callback said true for all of them.

With no items, the callback never runs. No item said false, so the answer is true. The language defines it this way, and every engine agrees. It is correct logic and a bad test.

Add the question the test forgot to ask:

const results = [];

const allAvailable = results.length > 0 && results.every((book) => book.available);
console.log(allAvailable);
false

&& means "and". The whole line is true only when the list has items and every item passes.

Add this question only where an empty list is wrong. Some lists should be empty. A list of error messages on a page that loaded correctly is one. For that list, every on its own gives the right answer.

The five methods you will use most

Each of these takes a callback and calls it once per item.

const books = [
  { title: "Dune", available: true },
  { title: "Emma", available: false },
  { title: "Ulysses", available: true },
];

const titles = books.map((book) => book.title);
const available = books.filter((book) => book.available);
const emma = books.find((book) => book.title === "Emma");

console.log(titles.join(", "));
console.log(available.length);
console.log(emma.available);
console.log(books.some((book) => book.available));
console.log(books.every((book) => book.available));
Dune, Emma, Ulysses
2
false
true
false
  1. map builds a new array from what the callback returns. Here it turns books into titles.
  2. filter builds a new array with only the items where the callback returned true.
  3. find gives the first item where the callback returned true.
  4. some is true when at least one item passes.
  5. every is true when all items pass.

join(", ") turns an array into one string, with the given text between the items.

What they return when nothing matches

This is the part that decides whether your test can fail.

const books = [];

const missing = books.find((book) => book.title === "Hamlet");
const matches = books.filter((book) => book.available);

console.log(missing);
console.log(matches.length);
console.log(Boolean(matches));
console.log(books.some((book) => book.available));
console.log(books.every((book) => book.available));
undefined
0
true
false
true
  • find gives undefined. The next line that reads a property from it stops with an error. The next lecture covers that error.
  • filter gives an empty array. An empty array counts as true in a condition, so if (matches) is always true. Check matches.length.
  • some gives false.
  • every gives true.

There is one more version of the same problem. A test reads the rows from the page, then checks that the count of rows equals the count of titles it collected from those same rows. Both numbers are 0 on a broken page, and 0 equals 0. Compare with a number your test data fixes, not with a second reading of the same page.

Remember this

every is true for an empty list, find gives undefined, and an empty filter result still counts as true. Before a check on a list, assert that the list has the items you expect.

Reference: Array.prototype.every on MDN.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.