JavaScript map()
The map()
method in JavaScript creates a new array with the results of calling a provided function on every element in the source array. In other words, this method iterates over the elements of an array and returns a new array containing the results of a callback function that is executed for each element.
Syntax
The syntax for using the map()
method is as follows:
var newArray = array.map(callback(currentValue[, index[, array]]) {
// return element for newArray, after executing something
}[, thisArg]);
newArray
is the new array that is returned by themap()
method.array
is the source array for themap()
method.callback
is the function that will be executed on every element of thearray
.currentValue
is the current element that is being processed by thecallback
.index
(optional) is the index of the current element being processed in thearray
.array
(optional) is the source array that is being processed.thisArg
(optional) is the value to be passed as 'this' when executing thecallback
function.
Example
Here is a simple example that illustrates the use of the map()
method: