Explanation: 2635. Apply Transform Over Each Element In Array
Arrays, Higher-Order Functions
Create a function that takes an array and a function where the function would be applied to every single element of the array and a new array would be returned with the new values. It should be solved without the Array.map
method and should pass the current element's value arr[i]
and index number i
to the transformation function (returnedArray[i] = fn(arr[i], i)
).
- Time Complexity: O(n) where
n
is the length of the array that is passed. Because each element in the array will be iterated through, the time complexity is dependent on the number of items.
const map = function (arr, fn) {
const newArray = [];
for (let i = 0; i < arr.length; i++) {
const newValue = fn(arr[i], i);
newArray.push(newValue);
}
return newArray;
};
The map
function transforms an array by:
- Creating an empty array to store the new values.
- Iterating through each element of the original array.
- Applying a function to each element.
- Storing the transformed values in the new array.
For example, if we start with the array [1, 2, 3, 4, 5]
and pass the function fn = (x) => x + 2
, the map function will:
- Loop through each element
- Apply the function to transform each value
- Store each new value in a new array
The original array remains untouched, and we receive a new array [3, 4, 5, 6, 7]
.
// Basic transformation
map([1, 2, 3, 4, 5], x => x + 2); // [3, 4, 5, 6, 7]
// Transformation using the index number
map([10, 20, 30], (x, i) => x + i); // [10, 21, 32]