Whenever you want to loop over an array and create a new array based on each element in the first array, it's possible to use the map
function on the array, instead of using a for
loop. There are a few advantages to map
over for
, including shorter code that is more composable - meaning you can easily extract and reuse the function(s) that you're using. You can also chain multiple map
calls together.