Array.prototype.map
JS Functions
Easy
10 min
arrays
polyfills
this
Implement Array.prototype.myMap(callbackFn, thisArg), a polyfill for the native Array.prototype.map. It returns a new array of the same length where each element is the result of callbackFn.
The starter attaches the function to Array.prototype and default-exports it.
[1, 2, 3].myMap((n) => n * 2); // [2, 4, 6]
['a', 'b'].myMap((ch, i) => ch + i); // ['a0', 'b1']
const scale = { factor: 10 };
[1, 2].myMap(function (n) {
return n * this.factor;
}, scale); // [10, 20]
[1, , 3].myMap((n) => n * 2); // [2, <hole>, 6]
Requirements
- Call
callbackFn(element, index, array)in ascending index order, withthisArgasthis. - Throw a
TypeErrorifcallbackFnis not callable, before iterating. - The result has the same length as the source. Holes are not visited and stay holes in the result (
1 in resultisfalseabove). - Read the length once. Elements appended during iteration are not visited. Elements deleted before they are reached become holes.
- Do not mutate the source. Do not use the native
map,forEach,reduceorArray.fromwith a mapper.
Follow-up
- Why does
['1', '2', '3'].map(parseInt)return[1, NaN, NaN]? - Why is
Array(3).map((_, i) => i)not[0, 1, 2], and how would you fix it?
Spotted a mistake or a rights concern? Report an issue