The map()
method of TypedArray
instances creates a new typed array populated with the results of calling a provided function on every element in the calling typed array. This method has the same algorithm as Array.prototype.map()
.
The map()
method of TypedArray
instances creates a new typed array populated with the results of calling a provided function on every element in the calling typed array. This method has the same algorithm as Array.prototype.map()
.
map(callbackFn) map(callbackFn, thisArg)
callbackFn
A function to execute for each element in the typed array. Its return value is added as a single element in the new typed array. The function is called with the following arguments:
thisArg
Optional
A value to use as this
when executing callbackFn
. See iterative methods.
A new typed array with each element being the result of the callback function.
See Array.prototype.map()
for more details. This method is not generic and can only be called on typed array instances.
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((num) => num * 2); // doubles is now Uint8Array [2, 8, 18] // numbers is still Uint8Array [1, 4, 9]
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
map |
45 | 12 | 38 | 32 | 9.1 | 45 | 38 | 32 | 9.3 | 5.0 | 45 | 1.0 | 4.0.0 |
© 2005–2023 MDN 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