Introduction
From the classic for loop to the forEach() method, there are various techniques and methods used to iterate through a data set in JavaScript. One of the most popular methods is the .map() method. .map() creates an array by calling a specific function on each item in the parent array. .map() is a non-mutation method that creates a new js array, unlike mutating methods, which only make changes to the calling array. This method can have many uses when working with arrays. In this tutorial, you will see four notable uses of .map() in JavaScript: calling a function from array elements, converting strings to arrays, rendering lists in JavaScript libraries, and reformatting array objects.
Prerequisites
This tutorial doesn't require coding, but if you want to follow along with the examples, you can use the Node.js REPL or browser developer tools.
How to call a JS function for each item in an array
.map() accepts a callback function as one of its arguments, and an important parameter of that function is the current value of the item processed by the function. This is a required parameter. Using this parameter, you can modify any item in an array and return it as the modified member of your new array.
Here is an example:
const sweetArray = [2, 3, 4, 5, 35]
const sweeterArray = sweetArray.map(sweetItem => {
return sweetItem * 2
})
console.log(sweeterArray)This output is logged in the console:
Output
[ 4, 6, 8, 10, 70 ]
This can be simplified to make it cleaner with the following:
// create a function to use
const makeSweeter = sweetItem => sweetItem * 2;
// we have an array
const sweetArray = [2, 3, 4, 5, 35];
// call the function we made. more readable
const sweeterArray = sweetArray.map(makeSweeter);
console.log(sweeterArray);The same output is logged in the console:
Output
[ 4, 6, 8, 10, 70 ]
Having code like sweetArray.map(makeSweeter) makes your code a little more readable.
How to convert a JS string to an array
map() belongs to the array prototype. In this step, you will use it to convert a string to an array. You will not develop a method for strings here. Instead, you will use the special .call() method.
Everything in JavaScript is an object, and methods are functions that are attached to these objects. Call() allows you to use the text of one object on another. So, you can copy the text of .map() into an array onto a string.
Call() can be passed context arguments and parameters to the main function arguments.
Here is an example:
const name = "Sammy"
const map = Array.prototype.map
const newName = map.call(name, eachLetter => {
return `${eachLetter}a`
})
console.log(newName)This output is logged in the console:
Output
[ "Sa", "aa", "ma", "ma", "ya" ]Here, you used the .map() function on a string and passed the function argument that .map() expects.
This works like the split() method of a string, except that each individual string character can be changed before being returned in an array.
How to render lists in JavaScript libraries
JavaScript libraries like React use .map() to render items in a list. This requires JSX syntax, however, because the .map() method is wrapped in JSX syntax.
Here is an example of a React component:
import React from "react";
import ReactDOM from "react-dom";
const names = ["whale", "squid", "turtle", "coral", "starfish"];
const NamesList = () => (
<div>
<ul>{names.map(name => <li key={name}> {name} </li>)}</ul>
</div>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<NamesList />, rootElement);This is a stateless React component that renders a div with a list. The individual list items are rendered using .map() to iterate over an array of names. The component is rendered using ReactDOM on the DOM element with the root ID.
How to reformat JavaScript array objects
.map() can be used to iterate through the objects in an array, modifying the contents of each individual object and returning a new array in a similar way to traditional arrays. This modification is done based on what is returned in the callback function.
Here is an example:
const myUsers = [
{ name: 'shark', likes: 'ocean' },
{ name: 'turtle', likes: 'pond' },
{ name: 'otter', likes: 'fish biscuits' }
]
const usersByLikes = myUsers.map(item => {
const container = {};
container[item.name] = item.likes;
container.age = item.name.length * 10;
return container;
})
console.log(usersByLikes);This output is logged in the console:
Output
[
{shark: "ocean", age: 50},
{turtle: "pond", age: 60},
{otter: "fish biscuits", age: 50}
]
Here, you have modified each object in the array using bracket and dot notation. This use case can be used to process or compress the received data before storing or parsing it in a front-end application.
Result
In this tutorial, we looked at four uses of the .map() method in JavaScript. In combination with other methods, the functionality of .map() can be extended.









