How to Use a Map in JavaScript: A Beginner's Guide
Learn how to use a map to modify an array of items in JavaScript
Introduction
Sometimes, we have a list of items and want to do something to each item in the list. For example, we might have a list of numbers, and we want to double each number in the list. Or, we might have a list of words and want to make each word uppercase.
One way to do this is to use a "map." A map is a way to loop over an array (a list of items) in JavaScript and modify each item in the array.
Using a map
Here's how to use a map in JavaScript:
- Start by creating an array of items. For example, let's say we have an array of numbers:
const numbers = [1, 2, 3, 4, 5];
Next, use the
.map()
Method on the array to create a new array. This method takes a function as an argument. This function is called for each item in the array and can be used to modify the item. For example, here's how we can use the.map()
method to double each number in the array:const doubledNumbers = numbers.map(function(number) { return number * 2; });
In this code, we're calling the
.map()
method on thenumbers
array. We're passing a function as an argument to the.map()
method. This function is called for each item in thenumbers
array. It takes the item (in this case, a number) as an argument and returns the result of multiplying the number by 2.
The
.map()
method returns a new array, which contains the modified items. In our example, thedoubledNumbers
variable will hold the new array of doubled numbers. We can log the new array to the console to check the result:console.log(doubledNumbers); // [2, 4, 6, 8, 10]
As you can see, the
doubledNumbers
array contains the original numbers from thenumbers
array, but each number has been doubled.Using arrow functions
In the previous example, we used a regular function as an argument to the
.map()
method. However, JavaScript also has a shorter syntax for creating functions called "arrow functions." Here's how we can rewrite the previous example using an arrow function:
const doubledNumbers = numbers.map(number => number * 2);
As you can see, this code is much shorter than the previous example. The arrow function takes the number
argument and returns the result of multiplying the number by 2.
Using a map with objects
In the previous examples, we used a map with an array of numbers. However, we can also use a map with an array of objects. For example, let's say we have an array of users where each user has a name
and an age
:
const users = [
{ name: "John", age: 30 },
{ name: "Jane", age: 25 },
{ name: "Bob", age: 40 }
];
We can use a map to loop over the array of users and modify each user object. For example, let's say we want to add a greeting
property to each user object, which contains a greeting message based on the user's name. Here's how we can do that:
const usersWithGreetings = users.map(function(user) {
const greeting = `Hello, ${user.name}!`;
return { ...user, greeting };
});
In this code, we're calling the .map()
method on the users
array. We're passing a function as an argument to the .map()
method. This function is called for each user in the users
Array. It takes the user object as an argument, and it returns a new object which contains the original user object and the new greeting
property.
We can log the new array of user objects to the console to check the result:
console.log(usersWithGreetings);
/*
[
{ name: "John", age: 30, greeting: "Hello, John!" },
{ name: "Jane", age: 25, greeting: "Hello, Jane!" },
{ name: "Bob", age: 40, greeting: "Hello, Bob!" }
]
*/
As you can see, the new array of user objects contains the original user objects, but each user object now also has a greeting
property.
Conclusion
This blog post taught us how to use a map in JavaScript. We saw how to use a map to loop over an array of items and modify each item. We also learned how to use arrow functions with a map and how to use a map with an array of objects.
Using a map is a convenient way to modify an array of items in JavaScript. It's valuable in your toolkit, and I hope this guide helps you understand how to use it.