Calls a defined callback function on each element of an array, and returns an array that contains the results.
Syntaxmap( callbackfn [, thisArg ])
A new array in which each element is the callback function return value for the associated original array element.
ExamplesThe following example illustrates the use of the map method.
function AreaOfCircle(radius) {
var area = Math.PI * (radius * radius);
return area.toFixed(0);
}
var radii = [10, 20, 30];
var areas = radii.map(AreaOfCircle);
document.write(areas);
The following example illustrates the use of the thisArg argument, which specifies an object to which the this keyword can refer.
var obj = {
divisor: 10,
remainder: function (value) {
return value % this.divisor;
}
}
var numbers = [6, 12, 25, 30];
var result = numbers.map(obj.remainder, obj);
document.write(result);
In the following example, a built-inJavaScript method is used as the callback function.
var numbers = [9, 16];
var result = numbers.map(Math.sqrt);
document.write(result);
The map method can be applied to a string. The following example illustrates this.
function threeChars(value, index, str) {
return str.substring(index - 1, index + 2);
}
var word = "Thursday";
var result = [].map.call(word, threeChars);
document.write(result);
Remarks
The map method calls the callbackfn function one time for each element in the array, in ascending index order. The callback function is not called for missing elements of the array.
In addition to array objects, the map method can be used by any object that has a length property and that has numerically indexed property names.
The syntax of the callback function is as follows:
function callbackfn(value, index, array1)
You can declare the callback function by using up to three parameters.
The following table lists the callback function parameters.
Callback argument Definition value The value of the array element. index The numeric index of the array element. array1 The array object that contains the element.The array object can be modified by the callback function.
The following table describes the results of modifying the array object after the map method starts.
Condition after the map method starts Element passed to callback function? Element is added beyond the original length of the array. No. Element is added to fill in a missing element of the array. Yes, if that index has not yet been passed to the callback function. Element is changed. Yes, if that element has not yet been passed to the callback function. Element is deleted from the array. No, unless that element has already been passed to the callback function. ExceptionsIf the callbackfn argument is not a function object, a TypeError exception is thrown.
See also Specification[15.4.4.19 Array.prototype.map ( callbackfn , thisArg )] ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4