Table of contents
No headings in the article.
If you need to access an element in an array, commonly we iterate through the array using loops, Instead of manually iterating over the array using a loop, you can simply use the built-in Array. map method.
The map method creates a new array with the results of calling a provided function on every element in the calling array. The map()
method takes two arguments: the first is a callback function that will be executed on each element in the array, and the second is the value to use when executing the callback. The callback function should return the value to be added to the new array. The map()
method does not modify the original array.
Example: Multiplying Each element in the array with two
Output
Here we got a new array called doubleNumber, this is the main advantage of the map function, We are not losing the parent array
We can optimize the above code
Output
Another Example: Reformatting Array Objects
Output
Here we Reformated the array called Name, by Finding the age of the person using dob, for that we used the Moment.js library