Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. TypedArray
  6. from()

TypedArray.from()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨September 2016⁩.

TheTypedArray.from() static method creates a newtyped arrayfrom an array-like or iterable object. This method is nearly the same asArray.from().

Try it

const uint16 = Int16Array.from("12345");console.log(uint16);// Expected output: Int16Array [1, 2, 3, 4, 5]

Syntax

js
TypedArray.from(arrayLike, mapFn)TypedArray.from(arrayLike, mapFn, thisArg)

WhereTypedArray is one of:

Parameters

arrayLike

An iterable or array-like object to convert to a typed array.

mapFnOptional

A function to call on every element of the typed array. If provided, every value to be added to the array is first passed through this function, andmapFn's return value is added to the typed array instead. 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.

thisArgOptional

Value to use asthis when executingmapFn.

Return value

A newTypedArray instance.

Description

SeeArray.from() for more details.

There are some subtle distinctions betweenArray.from() andTypedArray.from() (note: thethis value mentioned below is thethis value thatTypedArray.from() was called with, not thethisArg argument used to invokemapFn):

  • If thethis value ofTypedArray.from() is not a constructor,TypedArray.from() will throw aTypeError, whileArray.from() defaults to creating a newArray.
  • The object constructed bythis must be aTypedArray instance, whileArray.from() allows itsthis value to be constructed to any object.
  • When thesource parameter is an iterator,TypedArray.from() first collects all the values from the iterator, then creates an instance ofthis using the count, and finally sets the values on the instance.Array.from() sets each value as it receives them from the iterator, then sets itslength at the end.
  • TypedArray.from() uses[[Set]] whileArray.from() uses[[DefineOwnProperty]]. Hence, when working withProxy objects, it callshandler.set() to create new elements rather thanhandler.defineProperty().
  • WhenArray.from() gets an array-like which isn't an iterator, it respects holes.TypedArray.from() will ensure the result is dense.

Examples

From an iterable object (Set)

js
const s = new Set([1, 2, 3]);Uint8Array.from(s);// Uint8Array [ 1, 2, 3 ]

From a string

js
Int16Array.from("123");// Int16Array [ 1, 2, 3 ]

Use with arrow function and map

Using an arrow function as the map function to manipulate the elements

js
Float32Array.from([1, 2, 3], (x) => x + x);// Float32Array [ 2, 4, 6 ]

Generate a sequence of numbers

js
Uint8Array.from({ length: 5 }, (v, k) => k);// Uint8Array [ 0, 1, 2, 3, 4 ]

Calling from() on non-TypedArray constructors

Thethis value offrom() must be a constructor that returns aTypedArray instance.

js
function NotArray(len) {  console.log("NotArray called with length", len);}Int8Array.from.call({}, []); // TypeError: #<Object> is not a constructorInt8Array.from.call(NotArray, []);// NotArray called with length 0// TypeError: Method %TypedArray%.from called on incompatible receiver #<NotArray>
js
function NotArray2(len) {  console.log("NotArray2 called with length", len);  return new Uint8Array(len);}console.log(Int8Array.from.call(NotArray2, [1, 2, 3]));// NotArray2 called with length 3// Uint8Array(3) [ 1, 2, 3 ]

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-%typedarray%.from

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp