Skip to curriculum
Jump to the editor

3.2. Array.prototype.filter

Easy
Topics
10–20 min

filter picks the items you want from a list: the unread messages, the products in stock, the users who match a search. Writing it yourself shows how the built-in decides what counts as a pass, why its result never has holes, and what it does with array-like objects and bad arguments.

Write Array.prototype.myFilter(callback, thisArg) in filter.js. It must behave like the built-in Array.prototype.filter for every case below.

Signature

JAVASCRIPT
Array.prototype.myFilter = function myFilter(callback, thisArg) // returns a new array
  • callback(value, index, array): called once for each index the array has. array is the object myFilter was called on.
  • thisArg: the this of every callback call. When it is missing, the callback's this is undefined.
  • Returns a new, packed array (no holes) of the values the callback accepted, in their original order.

Requirements

  1. myFilter returns a new array, even when every item passes, and leaves the original array unchanged.
  2. An item is kept when the callback returns any truthy value (1, "yes", an object), and dropped for any falsy value (0, "", null, undefined, NaN).
  3. The callback receives the value, its index and the array (the same object), with thisArg as its this. The callback itself is called, as the built-in does, even when it has its own call property.
  4. Holes are skipped, and the result is packed: kept values sit at indexes 0, 1, 2… An index that holds undefined is an item, not a hole.
  5. length is read once, before the loop: items pushed during the call are not visited, and an item deleted before the loop reaches it is skipped.
  6. The value stored in the result is the value passed to the callback, even if the callback then changes the array.
  7. myFilter works on any object with a length (an array-like object, or a string), called with Array.prototype.myFilter.call(…). length is converted to a whole number from 0 up: "2" is 2, 2.9 is 2, -1 and a missing length are 0. A length that is a BigInt or a Symbol, or an object whose valueOf returns one, throws a TypeError, as the built-in does.
  8. It throws a TypeError when the callback is not a function (even for an empty array), and when it is called on null or undefined.
  9. An error thrown by the callback stops the loop and reaches the caller unchanged.

Examples

JAVASCRIPT
[1, 2, 3, 4, 5].myFilter((value) => value % 2 === 1); // [1, 3, 5]
[0, "a", "", null, 2].myFilter((value) => value);      // ["a", 2]: truthy results keep
JAVASCRIPT
const limits = { min: 10 };
[5, 10, 15].myFilter(function (value) { return value >= this.min; }, limits); // [10, 15]
JAVASCRIPT
[1, , 3].myFilter(() => true);   // [1, 3]: length 2, no hole
Array.prototype.myFilter.call("a1b2", (letter) => letter >= "0" && letter <= "9"); // ["1", "2"]
[1].myFilter(null);              // TypeError

Environment

  • Plain JavaScript in a browser page; the tests call Array.prototype.myFilter from filter.js.
  • filter.js starts with "use strict". Keep it: in strict mode, this inside myFilter is exactly the value it was called on, so a call on null can be detected.
  • Not allowed: the built-in filter, forEach, flatMap and reduce. The tests delete them from Array.prototype while myFilter runs. Write the loop yourself.
  • Out of scope: subclasses of Array (the built-in creates the result through Symbol.species; myFilter always returns a plain array).

Hints

  1. The result and the source have different indexes once something is dropped. What do you need to keep track of besides the loop index?
  2. If the callback writes to array[index], which value should end up in the result?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…