Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT JAVASCRIPT
3.1. Array.prototype.map
Easy
Topics
This question is in
3. Array methods from scratchmap 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
Array.prototype.myMap = function myMap(callback, thisArg) // returns a new arraycallback(value, index, array): called once for each index the array has.arrayis the objectmyMapwas called on.thisArg: thethisof every callback call. When it is missing, the callback'sthisisundefined.- Returns a new array with the same length as the source; each result is stored at the index of its value.
Requirements
myMapreturns a new array of the callback's results and leaves the original array unchanged.- The callback receives three arguments: the value, its index, and the array (the same object, not a copy).
- The callback runs with
thisArgas itsthis. The callback itself is called, as the built-in does, even when it has its owncallproperty. - Holes are skipped: the callback does not run for them, and the result has a hole at the same index. An index that holds
undefinedis not a hole. - The result has the source's length, even when the source has only holes.
lengthis 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.myMapworks on any object with alength(an array-like object, or a string), called withArray.prototype.myMap.call(…).lengthis converted to a whole number from 0 up:"2"is 2,2.9is 2,-1and a missing length are 0. Alengththat is a BigInt or a Symbol, or an object whosevalueOfreturns one, throws aTypeError, as the built-in does.- It throws a
TypeErrorwhen the callback is not a function (even for an empty array), and when it is called onnullorundefined. - An error thrown by the callback stops the loop and reaches the caller unchanged.
Examples
[1, 2, 3].myMap((value) => value * 2); // [2, 4, 6]
["a", "b"].myMap((value, index) => value + index); // ["a0", "b1"]const shipping = { fee: 40 };
[100, 250].myMap(function (price) { return price + this.fee; }, shipping); // [140, 290]const sparse = [1, , 3]; // index 1 is a hole
sparse.myMap((value) => value * 10); // [10, empty, 30]: length 3, index 1 still a holeArray.prototype.myMap.call({ 0: "a", 1: "b", length: 2 }, (value) => value.toUpperCase()); // ["A", "B"]
[1].myMap("not a function"); // TypeErrorEnvironment
- Plain JavaScript in a browser page; the tests call
Array.prototype.myMapfrommap.js. map.jsstarts with"use strict". Keep it: in strict mode,thisinsidemyMapis exactly the value it was called on, so a call onnullcan be detected.- Not allowed: the built-in
map,forEachandflatMap. The tests delete them fromArray.prototypewhilemyMapruns. Write the loop yourself. - Out of scope: subclasses of
Array(the built-in creates the result throughSymbol.species;myMapalways returns a plain array).
Hints
- How can you tell a hole from an index that holds
undefined? - 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…