I want to create a Uint8Array with 8 elements. I tried this
var myarr = new Uint8Array(255,255,255,255,40,92,143,2);
but when I run
myarr.length
I get back "255" when I expect the answer would be "8". I assume I'm doing somethign wrong in my intitialization step but don't know what that is.
The different constructors are:
new TypedArray(); // new in ES2017
new TypedArray(length);
new TypedArray(typedArray);
new TypedArray(object);
So when you do
new Uint8Array(255)
It will take it as a length. However you can pass an object (an array) instead which will be iterated to create the uint array:
new Uint8Array([255, 255, /*..*/]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With