JavaScript .map() Method: Create New Arrays with Transformed Elements
The '.map()' method in JavaScript is used to iterate over an array and create a new array with the results of calling a provided function on every element in the original array.
Here is an example of how to use '.map()':
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 called on the 'numbers' array and a callback function is provided as an argument. This callback function takes each element of the 'numbers' array as input and multiplies it by 2. The resulting array, 'doubledNumbers', will contain the doubled values of the original array.
原文地址: https://www.cveoy.top/t/topic/wqz 著作权归作者所有。请勿转载和采集!