It’s much more pleasant to use it now than ever.
from static method lets us convert an array-like or iterable object into a typed array.
The first argument is the iterable object we want to convert to a typed array.
The 2nd is a callback that has the value of the entry and the index as the parameter of the array to let us return something we want.
The 3rd argument is the value of
this we want to have in the callback in the 2nd argument.
The 2nd and 3rd arguments are optional.
For example, we can write:
const typedArr = Uint16Array.from([0, 1, 2]);
We pass in the array value and returns a
Uint16Array with the entries from the array.
The callback for mapping the entries let us create a new array that doesn’t overflow rather than have them overflow.
For example, instead of writing:
const arr = Int8Array.of(200, 201, 202).map(x => 2 * x)
And get the
Int8Array with values
[-112, -110, -108] .
We can write:
const arr = Int32Array.from(Int8Array.of(120, 121, 122), x => x * 2)
[240, 242, 244] in an
There are many instance properties included with typed arrays.
buffer property is a getter than returns the buffer that’s storing the data of the typed array.
byteLength property is a number that returns the size in bytes of the typed array’s buffer.
byteOffset returns the offset where the typed array starts inside the ArrayByffr.
set copies all elements of an array or typed array into the given typed array.
If it’s a normal array, then all elements are converted to numbers.
If the argument is a typed array, then each element is converted to the type appropriate for the typed array.
subarray returns a new typed array that has the same buffer as the typed array it’s called on.
It takes the beginning index as the first argument. The default value of this is 0.
The end index is its second argument. The default value of this is the
length of the array.
The values can be negative.
Array methods like
toString , and
values are all available to typed arrays.
They take the same arguments and have the same return values as regular array methods.
Typed arrays constructors include
They can have the signature
(buffer, byteOffset=0, length?) ,
(arrayLikeObject) , or
buffer is the buffer of a typed array.
ByteOffset is the offset from index 0.
length is the length of the typed array.
typedArray is another typed array.
arrayLikeObject is an array-like or iterable object.
An array-like object is one with the
length property and integer keys.
Typed arrays have many array methods.
The constructor is different from the regular