16

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

delux
  • 1,694
  • 10
  • 33
  • 64

2 Answers2

25

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)
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
13

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];
Rob Brander
  • 3,702
  • 1
  • 20
  • 33
  • 11
    Better use `Array.from(typedArray)`, it's more clear about the purpose – Bergi Sep 18 '18 at 17:58
  • 1
    Anyone wondering why this is needed one must convert a typed array (i.e. Uint8array) to an Array object. typed arrays are not Array's in javascript. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays – kuzdogan Oct 13 '20 at 14:17