This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The map() method is used to iterate over an array and modify its elements using a callback function. This callback function will run on each array element and return a new array of modified elements. | |
``` | |
// Syntax | |
myArray.map(callbackFn) | |
myArray.map(function(element, index, array){ /* ... */ }) | |
``` | |
For example: | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is one of the most powerful JavaScript array methods because it applies a reducer function to each element of your array and returns a single output. The reducer function iterates through all elements in your array from left to right (in order) and returns the result of the previous element’s calculation. | |
Thus, the final result is a single value. This single value is the function’s accumulated result after executing a reducer function for every array element. | |
``` | |
// Syntax | |
myArray.reduce(callbackFn, initialValue) | |
``` | |
For example: |
OlderNewer