Core JavaScript 1.5 Reference:Global Objects:Array:map
From MDC
Contents |
[edit] Summary
Creates a new array with the results of calling a provided function on every element in this array.
| Method of Array | |
| Implemented in: | JavaScript 1.6 (Gecko 1.8b2 and later) |
| ECMAScript Edition: | none |
[edit] Syntax
var mappedArray = array.map(callback[, thisObject]);
[edit] Parameters
-
callback - Function produce an element of the new Array from an element of the current one.
-
thisObject - Object to use as
thiswhen executingcallback.
[edit] Description
map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.
If a thisObject parameter is provided to map, it will be used as the this for each invocation of the callback. If it is not provided, or is null, the global object associated with callback is used instead.
map does not mutate the array on which it is called.
The range of elements processed by map is set before the first invocation of callback. Elements which are appended to the array after the call to map begins will not be visited by callback. If existing elements of the array are changed, or deleted, their value as passed to callback will be the value at the time map visits them; elements that are deleted are not visited.
[edit] Compatibility
map is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of map in ECMA-262 implementations which do not natively support it. This algorithm is exactly the one used in Firefox and SpiderMonkey.
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
[edit] Examples
[edit] Example: Pluralizing the words (strings) in an array
The following code creates an array of "plural" forms of nouns from an array of their singular forms.
function makePseudoPlural(single)
{
return single.replace(/o/g, "e");
}
var singles = ["foot", "goose", "moose"];
var plurals = singles.map(makePseudoPlural);
// plurals is ["feet", "geese", "meese"]
// singles is unchanged
[edit] Example: Mapping an array of numbers to an array of square roots
The following code takes an array of numbers and creates a new array containing the square roots of the numbers in the first array.
var numbers = [1, 4, 9]; var roots = numbers.map(Math.sqrt); // roots is now [1, 2, 3] // numbers is still [1, 4, 9]
[edit] Example: using map generically
This example shows how to use map on a string to get an array of bytes in the ASCII encoding representing the character values:
var a = Array.prototype.map.call("Hello World",
function(x) { return x.charCodeAt(0); })
// a now equals [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]