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.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
typedArray.map(mapFn[, thisArg])
mapFn
A callback function that produces an element of the new typed array, taking three arguments:
currentValue
The current element being processed in the typed array.
index
可选
The index of the current element being processed in the typed array.
array
可选
map()
was called upon.
thisArg
可选
this
when executing
mapFn
.
A new typed array.
map()
method calls a provided callback function (
mapFn
) once for each element in a typed array, in order, and constructs a new typed array from the results.
mapFn
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.
mapFn
is invoked with three arguments: the value of the element, the index of the element, and the typed array object being traversed.
若
thisArg
parameter is provided to
map()
, it will be passed to
mapFn
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
mapFn
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
mapFn
, if invoked, may do so).
The range of elements processed by
map()
is set before the first invocation of
mapFn
. Elements which are appended to the array after the call to
map()
begins will not be visited by
mapFn
. If existing elements of the typed array are changed, or deleted, their value as passed to
mapFn
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.
const numbers = new Uint8Array([1, 4, 9]); const 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.
const numbers = new Uint8Array([1, 4, 9]);
const doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now Uint8Array [2, 8, 18]
// numbers is still Uint8Array [1, 4, 9]
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'TypedArray.prototype.map' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
map
|
Chrome 45 | Edge 14 | Firefox 38 | IE No | Opera 32 | Safari No | WebView Android 45 | Chrome Android 45 | Firefox Android 38 | Opera Android 32 | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
完整支持
不支持
TypedArray
TypedArray.from()
TypedArray.of()
TypedArray.prototype.copyWithin()
TypedArray.prototype.entries()
TypedArray.prototype.every()
TypedArray.prototype.fill()
TypedArray.prototype.filter()
TypedArray.prototype.find()
TypedArray.prototype.findIndex()
TypedArray.prototype.forEach()
TypedArray.prototype.includes()
TypedArray.prototype.indexOf()
TypedArray.prototype.join()
TypedArray.prototype.keys()
TypedArray.prototype.lastIndexOf()
TypedArray.prototype.map()
TypedArray.prototype.reduce()
TypedArray.prototype.reduceRight()
TypedArray.prototype.reverse()
TypedArray.prototype.set()
TypedArray.prototype.slice()
TypedArray.prototype.some()
TypedArray.prototype.sort()
TypedArray.prototype.subarray()
TypedArray.prototype.toLocaleString()
TypedArray.prototype.toString()
TypedArray.prototype.values()
TypedArray.prototype[@@iterator]()
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
BigInt64Array
BigUint64Array
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()