Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypedArray.prototype.map()

BaselineWidely available

Themap() method ofTypedArray 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 asArray.prototype.map().

Try it

const uint8 = new Uint8Array([25, 36, 49]);const roots = uint8.map(Math.sqrt);console.log(roots);// Expected output: Uint8Array [5, 6, 7]

Syntax

js
map(callbackFn)map(callbackFn, thisArg)

Parameters

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:

element

The current element being processed in the typed array.

index

The index of the current element being processed in the typed array.

array

The typed arraymap() was called upon.

thisArgOptional

A value to use asthis when executingcallbackFn. Seeiterative methods.

Return value

A new typed array with each element being the result of the callback function.

Description

SeeArray.prototype.map() for more details. This method is not generic and can only be called on typed array instances.

Examples

Mapping a typed array to a typed array of square roots

The following code takes a typed array and creates a new typed array containing thesquare roots of the numbers in the first typed array.

js
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]

Mapping a typed array of numbers using a function containing an argument

The following code shows howmap() works when a function requiring oneargument is used with it. The argument will automatically be assigned to each element ofthe typed array asmap() loops through the original typed array.

js
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]

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-%typedarray%.prototype.map

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp