The 'map' method in JavaScript is used to iterate over an array and perform a specific operation on each element of the array. It creates a new array that contains the results of applying the operation to each element.

Here is an example of using the 'map' method in JavaScript:

const numbers = [1, 2, 3, 4, 5];

const doubledNumbers = numbers.map((num) => {
  return num * 2;
});

console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]

In this example, the 'map' method is used to iterate over the 'numbers' array and multiply each element by 2. The resulting array, 'doubledNumbers', contains the doubled values of the original array.

Note that the 'map' method does not modify the original array; it returns a new array with the modified values.

JavaScript Array map() Method: Explained with Example

原文地址: https://www.cveoy.top/t/topic/wAo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录