Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an array from ArrayBuffer?

I have an ArrayBuffer which looks like: enter image description here

This buffer is placed under variable named myBuffer and what I'm interested in, is to get the Uint8Array from this object.

I tried to loop as:

myBuffer.forEach(function(element) {
    console.log(element);
});

and to directly access to the Array as:

console.log(myBuffer['[[Uint8Array]]']);
console.log(myBuffer['Uint8Array']);

but seems none of this is the correct approach

like image 406
delux Avatar asked Sep 18 '18 17:09

delux


2 Answers

Those pseudo-properties you are seeing are something the developer console is putting there for your benefit. They aren't really there at all, as a property or a symbol (AFAIK), and even if they were it would be non-standard.

You can easily get a Uint8Array view of your buffer the standard way like this though:

new Uint8Array(myBuffer)
like image 189
Alexander O'Mara Avatar answered Oct 10 '22 21:10

Alexander O'Mara


You will first need to convert the array buffer into a typed array, then from there you can use the spread operator to create an array

const typedArray = new Uint8Array(myBuffer);
const array = [...typedArray];
like image 41
Rob Brander Avatar answered Oct 10 '22 21:10

Rob Brander