The Value of Map

Bradley Calkins
2 min readJan 5, 2021

The map function is a valueable applicative function in javascript. This function iterates through each element in an array and executes a function on each element. Then creates a new array in a non-mutative process.

The method in which a map function enacts its behavior on each element is via a callback function. For comprehension its advisable to look under the hood of the function. The logic is a standard for loop, and for context lets have a look at this bank array example…..

First, we declare an empty array to place the desired elements into this new array. Then to iterate and execute the code we will use a the for loop construct. In this case the new array will be all the bank names from the original array. The callback function is the block of code on line 20 and executed for each iterated element. Once we run our code we will console.log(bank_names) which gives us…..

However, writing for loops like this for each time you would like to abstract a new array is tedious. This is why the map function is valuable as we can execute an equal abstraction of data with less input. For instance…..

We simply save the map function to a variable and call the map function on the banks array. Then return which element we would like to abstract from the original array. The new bank_names array prints in the console.

--

--