Skip to curriculum
Jump to the editor

3.1. Array.prototype.map

Easy
Topics
10–20 min

map is the array method you call most: turn a list of users into a list of names, a list of prices into a list of labels. Writing it yourself shows what the built-in really does: which indexes it visits, what it passes to your callback, and what it does with holes, array-like objects and bad arguments.

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

Signature

JAVASCRIPT
Array.prototype.myMap = function myMap(callback, thisArg) // returns a new array
  • callback(value, index, array): called once for each index the array has. array is the object myMap was called on.
  • thisArg: the this of every callback call. When it is missing, the callback's this is undefined.
  • Returns a new array with the same length as the source; each result is stored at the index of its value.

Requirements

  1. myMap returns a new array of the callback's results and leaves the original array unchanged.
  2. The callback receives three arguments: the value, its index, and the array (the same object, not a copy).
  3. The callback runs 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: the callback does not run for them, and the result has a hole at the same index. An index that holds undefined is not a hole.
  5. The result has the source's length, even when the source has only holes.
  6. length is read once, before the loop: items pushed during the call are not visited. An item deleted before the loop reaches it is skipped, and a value changed before the loop reaches it is seen as changed.
  7. myMap works on any object with a length (an array-like object, or a string), called with Array.prototype.myMap.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].myMap((value) => value * 2);           // [2, 4, 6]
["a", "b"].myMap((value, index) => value + index); // ["a0", "b1"]
JAVASCRIPT
const shipping = { fee: 40 };
[100, 250].myMap(function (price) { return price + this.fee; }, shipping); // [140, 290]
JAVASCRIPT
const sparse = [1, , 3];                  // index 1 is a hole
sparse.myMap((value) => value * 10);      // [10, empty, 30]: length 3, index 1 still a hole
JAVASCRIPT
Array.prototype.myMap.call({ 0: "a", 1: "b", length: 2 }, (value) => value.toUpperCase()); // ["A", "B"]
[1].myMap("not a function");              // TypeError

Environment

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

Hints

  1. How can you tell a hole from an index that holds undefined?
  2. Which value should the loop stop at if the callback pushes onto the array while you loop?

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…