map function example

PHOTO EMBED

Wed Mar 15 2023 15:21:42 GMT+0000 (Coordinated Universal Time)

Saved by @bhushan03

In JavaScript, the map() function is used to transform an array by applying a provided function to each element of the array. It creates a new array with the same number of elements as the original array, but with each element transformed by the function.

Here's an example of using the map() function in JavaScript:

In this example, we define an array of numbers called nums, and then use the map() function to create a new array called doubledNums with each element doubled. The map() function takes a callback function as an argument, which is defined using an arrow function syntax. The num parameter in the arrow function represents the current element of the array being processed. The function returns the transformed element, which in this case is num * 2.

The map() function is useful when you want to transform an array without modifying the original array. It is often used in combination with other methods like filter() and reduce() to perform more complex operations on arrays. Note that the map() function returns a new array and does not modify the original array.
content_copyCOPY

javascript Copy code const nums = [1, 2, 3, 4, 5]; const doubledNums = nums.map(num => { return num * 2; }); console.log(doubledNums); // [2, 4, 6, 8, 10]

https://chat.openai.com/chat/807b8b90-cca8-4243-bfb7-b7de91cf8b16