Map .

Array Map Javascript: A Comprehensive Guide

Written by Pauline Lafleur Mar 19, 2022 · 5 min read
Array Map Javascript: A Comprehensive Guide

Table of Contents

Javascript Map() como mapear elementos de um array? Blog da Trybe
Javascript Map() como mapear elementos de um array? Blog da Trybe from blog.betrybe.com
sessions and code snippets.

Introduction

If you're a developer who works with Javascript, then you're probably familiar with arrays. Arrays are a fundamental data structure in programming, and they're used to store a collection of values in a single variable. But have you ever heard of "array map" in Javascript? If not, then you're missing out on one of the most powerful array methods available. In this article, we'll take a deep dive into the array map method in Javascript. We'll discuss what it is, how it works, and why it's so useful. We'll also provide some practical examples of how you can use array map in your own code.

What is Array Map?

Array map is a built-in method in Javascript that allows you to apply a function to each element in an array. The method takes a callback function as its argument, and this callback function is called on each element in the array. The result of each function call is then collected into a new array, which is returned by the map method. Here's an example that demonstrates how array map works: ```javascript const numbers = [1, 2, 3, 4, 5]; const squares = numbers.map(num => num * num); console.log(squares); // [1, 4, 9, 16, 25] ``` In this example, we're using array map to square each number in the `numbers` array. The `num => num * num` function is the callback function that's passed to the map method. This function takes a single argument (`num`) and returns the square of that number. The result of each function call is then collected into a new array (`squares`), which is logged to the console.

Why is Array Map Useful?

Array map is incredibly useful because it allows you to transform an array of one type into an array of another type. For example, you could use array map to transform an array of strings into an array of numbers, or vice versa. You could also use array map to transform an array of objects into an array of some other type of object. Another benefit of array map is that it's a pure function. This means that it doesn't modify the original array; instead, it returns a new array with the transformed values. This makes it very easy to reason about and test. Additionally, because array map returns a new array, you can chain it together with other array methods like filter and reduce to create powerful data transformations.

Practical Examples

Let's look at some practical examples of how you can use array map in your own code. Example 1: Converting Strings to Numbers ```javascript const strings = ['1', '2', '3', '4', '5']; const numbers = strings.map(str => parseInt(str)); console.log(numbers); // [1, 2, 3, 4, 5] ``` In this example, we're using array map to convert an array of strings into an array of numbers. We're using the `parseInt` function as the callback function to convert each string to a number. The result is a new array (`numbers`) that contains the converted values. Example 2: Getting the Length of Strings ```javascript const words = ['apple', 'banana', 'cherry', 'date', 'elderberry']; const lengths = words.map(word => word.length); console.log(lengths); // [5, 6, 6, 4, 10] ``` In this example, we're using array map to get the length of each string in an array. The `word.length` expression is the callback function that's used to get the length of each word. The result is a new array (`lengths`) that contains the lengths of each string. Example 3: Transforming Objects ```javascript const people = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 } ]; const formattedPeople = people.map(person => ({ fullName: `${person.name} Smith`, birthYear: new Date().getFullYear() - person.age })); console.log(formattedPeople); /* [ { fullName: 'Alice Smith', birthYear: 1998 }, { fullName: 'Bob Smith', birthYear: 1993 }, { fullName: 'Charlie Smith', birthYear: 1988 } ] */ ``` In this example, we're using array map to transform an array of objects into an array of new objects with a different shape. The callback function takes each person object and transforms it into a new object with a `fullName` property (which is the person's name with "Smith" added to the end) and a `birthYear` property (which is calculated based on the person's age). The result is a new array (`formattedPeople`) that contains the transformed objects.

Question & Answer

Q: What's the difference between array map and forEach? A: Array map and forEach are both array methods in Javascript, but they have different purposes. Array map is used to transform an array into a new array with the same length, whereas forEach is used to perform an operation on each element in an array. Additionally, array map returns a new array, whereas forEach doesn't return anything. Q: Can I use array map on nested arrays? A: Yes, you can use array map on nested arrays. You would just need to use nested callbacks to access the inner arrays, like this: ```javascript const nestedArray = [[1, 2], [3, 4], [5, 6]]; const flattenedArray = nestedArray.map(innerArray => innerArray.map(num => num * 2) ).flat(); console.log(flattenedArray); // [2, 4, 6, 8, 10, 12] ``` In this example, we're using array map on a nested array of numbers. The outer callback function is used to access each inner array, and the inner callback function is used to multiply each number by 2. The result is a flattened array with all of the multiplied values.
Read next