The TypedArray.from()
method creates a new typed array from an array-like or iterable object. This method is nearly the same as Array.from()
.
Syntax
TypedArray.from(source[, mapFn[, thisArg]])
where TypedArray is one of:
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
Parameters
source
- An array-like or iterable object to convert to a typed array.
mapFn
- Optional. Map function to call on every element of the typed array.
thisArg
- Optional. Value to use as
this
when executingmapFn
.
Description
TypedArray.from()
lets you create typed arrays from:
- array-like objects (objects with a
length
property and indexed elements) or - iterable objects (objects where you can get its elements, such as
Map
andSet
).
TypedArray.from()
has an optional parameter mapFn
, which allows you to execute a map
function on each element of the typed array (or subclass object) that is being created. More clearly, TypedArray.from(obj, mapFn, thisArg)
is the same as TypedArray.from(obj).map(mapFn, thisArg)
.
The length
property of the from()
method is 1.
Some subtle dinstinctions between Array.from()
and TypedArray.from()
:
- If the
|this|
value passed toTypedArray.from
is not a constructor,TypedArray.from
will throw aTypeError
, whereArray.from
defaults to creating a newArray
. TypedArray.from
uses[[Put]]
whereArray.from
uses[[DefineProperty]]
. Hence, when working withProxy
objects, it callshandler.set
to create new elements rather thanhandler.defineProperty
.- When
from
gets an iterator, theTypedArray
version first collects all the values from the iterator, then creates an instance of|this|
using the count, then sets the values on the instance.Array.from
sets each value as it gets them from the iterator then sets the length at the end. - When
Array.from
gets an array-like which isn't an iterator, it respects holes, whereTypedArray.from
will ensure the result is dense.
Examples
// Set (iterable object) var s = new Set([1, 2, 3]); Uint8Array.from(s); // Uint8Array [ 1, 2, 3 ] // String Int16Array.from("123"); // Int16Array [ 1, 2, 3 ] // Using an arrow function as the map function to // manipulate the elements Float32Array.from([1, 2, 3], x => x + x); // Float32Array [ 2, 4, 6 ] // Generate a sequence of numbers Uint8Array.from({length: 5}, (v, k) => k); // Uint8Array [ 0, 1, 2, 3, 4 ]
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of '%TypedArray%.from' in that specification. |
Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of '%TypedArray%.from' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 45.0 | 38 (38) | No support | No support | No support |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | No support | 38.0 (38) | No support | No support | No support |