Consider the following example:

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
Copy the code

In the above method, we return the result of an array map.

Reading,

The map() method returns a new array whose elements are the values processed by the call to the original array element.

The map() method processes the elements in their original array order.

Map () does not detect an empty array, and map() does not alter the original array.

The map() method traverses the array of primiples once, fetching the values in the array each time, and then calculating the values.

How this is done depends on the methods defined in the map function. If the above example uses arrow expressions to do this, please refer to the related article if you are not familiar with arrow expressions.

Of course, we can also define a function in the map, such as the following code:

const numbers = [65, 44, 12, 4];
const newArr = numbers.map(myFunction)

function myFunction(num) {
  return num * 10;
}
Copy the code

When the map method executes, it takes a value from the original array numbers and passes that value to myFunction.

MyFunction evaluates and populates the returned value back into the array to be returned with the value already extracted.

For this method, all we need to know is that we only need to perform the operations in the function definition for each of the input arrays.

www.ossez.com/t/javascrip…