Transforming with map in JavaScript

Say we have an array of objects representing scripts, produced by filtering the SCRIPTS array somehow. But we want an array of names, which is easier to inspect.

The map method transforms an array by applying a function to all of its elements and building a new array from the returned values. The new array will have the same length as the input array, but its content will have been mapped to a new form by the function.

function map(array, transform) {

let mapped = [];

for (let element of array) {



return mapped;


let rtlScripts = SCRIPTS.filter(s => s.direction == “rtl”);

console.log(map(rtlScripts, s =>;

// → [“Adlam”, “Arabic”, “Imperial Aramaic”, …]

Like forEach and filter, map is a standard array method.

Source: Haverbeke Marijn (2018), Eloquent JavaScript: A Modern Introduction to Programming,

No Starch Press; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *