Implement the Array.prototype.map method by hand.

function map(arr, mapCallback) {
// First, we check if the parameters passed are right.
if (!Array.isArray(arr) || !arr.length || typeof mapCallback !== 'function') {
return [];
} else {
let result = [];
// We're making a results array every time we call this function
// because we don't want to mutate the original array.
for (let i = 0, len = arr.length; i < len; i++) {
result.push(mapCallback(arr[i], i, arr));
// push the result of the mapCallback in the 'result' array
}
return result; // return the result array
}
}

As the MDN description of the Array.prototype.map method: The map() method creates a new array with the results of calling a provided function on every element in the calling array.


Author of content
Edit this page

Made by Michael Sakhniuk

Twitter