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()
.
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()
.
// Arrow function TypedArray.from(arrayLike, (element) => { /* ... */ } ) TypedArray.from(arrayLike, (element, index) => { /* ... */ } ) // Mapping function TypedArray.from(arrayLike, mapFn) TypedArray.from(arrayLike, mapFn, thisArg) // Inline mapping function TypedArray.from(arrayLike, function mapFn(element) { /* ... */ }) TypedArray.from(arrayLike, function mapFn(element, index) { /* ... */ }) TypedArray.from(arrayLike, function mapFn(element) { /* ... */ }, thisArg) TypedArray.from(arrayLike, function mapFn(element, index) { /* ... */ }, thisArg)
Where TypedArray
is one of:
arrayLike
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 executing mapFn
.
A new TypedArray
instance.
TypedArray.from()
lets you create typed arrays from:
length
property and indexed elements); or Map
and Set
). TypedArray.from()
has the 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. This means that the following are equivalent:
TypedArray.from(obj, mapFn, thisArg)
TypedArray.from(Array.prototype.map.call(obj, mapFn, thisArg))
.The length
property of the from()
method is 1
.
Some subtle distinctions between Array.from()
and TypedArray.from()
:
thisArg
value passed to TypedArray.from()
is not a constructor, TypedArray.from()
will throw a TypeError
, where Array.from()
defaults to creating a new Array
. TypedArray.from()
uses [[Set]]
where Array.from()
uses [[DefineOwnProperty]]
. Hence, when working with Proxy
objects, it calls handler.set()
to create new elements rather than handler.defineProperty()
.source
parameter is an iterator, the TypedArray.from()
first collects all the values from the iterator, then creates an instance of thisArg
using the count, then sets the values on the instance. Array.from()
sets each value as it receives them from the iterator, then sets its length
at the end. Array.from()
gets an array-like which isn't an iterator, it respects holes. TypedArray.from()
will ensure the result is dense. const s = new Set([1, 2, 3]); Uint8Array.from(s); // Uint8Array [ 1, 2, 3 ]
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 ]
Uint8Array.from({length: 5}, (v, k) => k); // Uint8Array [ 0, 1, 2, 3, 4 ]
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
from |
45 |
12 |
38 |
No |
32 |
10 |
45 |
45 |
38 |
32 |
10 |
5.0 |
1.0 |
4.0.0 |
© 2005–2022 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/from