19

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

2
  • 3
    Just new Uint8Array(myBuffer) Commented Sep 18, 2018 at 17:40
  • The "properties" enclosed in [[…]] (and also __proto__) are just a rendering of internal values by the devtools inspector. They don't actually exist on the object. Commented Sep 18, 2018 at 17:57

2 Answers 2

31

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)
Sign up to request clarification or add additional context in comments.

Comments

14

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];

2 Comments

Better use Array.from(typedArray), it's more clear about the purpose
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: developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.