The map()
method creates a new typed array with the results of calling a provided function on every element in this typed array. This method has the same algorithm as Array.prototype.map()
. TypedArray is one of the typed array types here.
typedarray.map(callback[, thisArg])
callback
currentValue
index
array
map
was called upon.thisArg
this
when executing callback
.A new typed array.
The map
method calls a provided callback
function once for each element in a typed array, in order, and constructs a new typed array from the results. callback
is invoked only for indexes of the typed array which have assigned values; it is not invoked for indexes that are undefined, those 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 typed array object being traversed.
If a thisArg
parameter is provided to map
, it will be passed to callback
when invoked, for use as its this
value. Otherwise, the value undefined
will be passed for use as its this
value. The this
value ultimately observable by callback
is determined according to the usual rules for determining the this
seen by a function.
map
does not mutate the typed array on which it is called (although callback
, if invoked, may do so).
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 typed 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.
The following code takes a typed array and creates a new typed array containing the square roots of the numbers in the first typed array.
var numbers = new Uint8Array([1, 4, 9]); var roots = numbers.map(Math.sqrt); // roots is now: Uint8Array [1, 2, 3], // numbers is still Uint8Array [1, 4, 9]
The following code shows how map works when a function requiring one argument is used with it. The argument will automatically be assigned to each element of the typed array as map loops through the original typed array.
var numbers = new Uint8Array([1, 4, 9]); var doubles = numbers.map(function(num) { return num * 2; }); // doubles is now Uint8Array [2, 8, 18] // numbers is still Uint8Array [1, 4, 9]
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'TypedArray.prototype.map' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'TypedArray.prototype.map' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | 14 | 38 | No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | No | Yes | Yes | 38 | No | No | Yes |
Server | |
---|---|
Node.js | |
Basic support | 4.0.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/map