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.
October 08, 2022
1049
Read more
What is the JavaScript += Operator and How Do You Use It?
December 04, 2022
JavaScriptUsing the startsWith() Method in JavaScript
December 04, 2022
JavaScriptReverse a String in JavaScript: 2 Easy Methods
December 02, 2022
JavaScript