How to get an array from ArrayBuffer?

一笑奈何 提交于 2020-11-29 04:47:04

问题


I have an ArrayBuffer which looks like:

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


回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/52391921/how-to-get-an-array-from-arraybuffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!