Do You Even .map?

Alexis Grisham
1 min readAug 20, 2018

--

I. Hate. Mapping. (Or rather, mapping hates me!!) A simple concept meant to make life easier… but does it?

Basically, ‘.map’ takes an array[] and does something(whatever function you call to it) to each value within the array.

EXAMPLE TIME! Say you have an array of objects, of first and last names.

classMates[ {firstName: “Chris”, lastName: “Packett”}, {firstName: “Manuel”, lastName: “Solis”}, {firstName: “Michael”, lastName: “Solone”}]

Now, we want to take each of those ‘values’(firstName and lastName) and combine them to get a new array, a list of each fullName.

function getFullName(item, index) {
var fullName = [item.firstName,item.lastName].join(“ “);
return fullName;
}

Now here’s where .map comes in:

function myClassMates() {
document.getElementById(“ex”).innerHTML = persons.map(getFullName);
}

The .map function calls to getFullName, and pulls out each piece that that function separated and then joined. So if we were to run this function, it would now return us with myClassMates[“Chris Packett”, “Manuel Solis”, “Michael Solone”].

This can also work on functions if you were given an array of numbers and wanted to multiply each of them by two, it would look something like .map((i) * 2).

Make sense yet?

--

--