Map

Map is a higher-order function that allows us to map a callback to each element of a collection. It is particularly useful when translating all elements of an array from one set of values to another. Here is a simple code example:

function myJS(){    let array = [1, 2, 3];    let arrayPlusTwo = array.map(current => current + 2);    // arrayPlusTwo == [3, 4, 5]}

This technique makes it possible to avoid using structural loops as much as possible when simply modifying the values of an array.

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.