← flaviocopes.com

The JavaScript map() Function

Published Jun 23 2019

Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting on FEBRUARY 01, 2023! SIGNUPS ARE NOW OPEN to this 10-weeks cohort course. Learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! ✨

map() is key method of an array when it comes to thinking in functional programming terms.

This example iterates a and builds a new array with the result of executing f() on each a element:

const b = a.map(f)

Given an array, we can use map() to create a new array from the initial one, and then filtering the result using filter(). This short example creates a new array to get the first letter of each item in the list array, and filters the one that matches A:

const list = ['Apple', 'Orange', 'Egg']
list.map(item => item[0]).filter(item => item === 'A') //'A'

I write books for developers 👇🏼

© 2023 Flavio Copes Flavio Copes made in Italy 🇮🇹 using Notion to Site Notion to Site