Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Array Methods (map) | Section
JavaScript Essentials for React Native Development

bookArray Methods (map)

Glissez pour afficher le menu

The map method is a powerful tool for transforming arrays in JavaScript. It allows you to create a new array by applying a function to each element of an existing array. The original array remains unchanged, making map a non-mutating method.

12345
const numbers = [1, 2, 3, 4]; const doubled = numbers.map(function(number) { return number * 2; }); console.log(doubled); // Output: [2, 4, 6, 8]
copy

When you call array.map(callback), the callback function is executed once for every element in the array. The callback receives up to three arguments:

  • The current element value;
  • The current element's index;
  • The original array.

Most commonly, you will use only the first parameter—the current value. The function you provide should return a value, which will become the corresponding element in the new array.

This makes map ideal for tasks like:

  • Converting numbers to strings;
  • Modifying each object in an array of objects;
  • Extracting a specific property from each object in an array.

Since map always returns a new array, you can chain it with other array methods or use it to keep your data transformations clear and predictable.

question mark

What does the map method return?

Sélectionnez la réponse correcte

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Section 1. Chapitre 7
some-alt