简体   繁体   English

如何从ArrayBuffer获取数组?

[英]How to get an array from ArrayBuffer?

I have an ArrayBuffer which looks like: 我有一个ArrayBuffer,看起来像: 在此输入图像描述

This buffer is placed under variable named myBuffer and what I'm interested in, is to get the Uint8Array from this object. 这个缓冲区放在名为myBuffer变量下,我感兴趣的是从这个对象获取Uint8Array

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 但似乎没有一个是正确的方法

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. 它们根本不存在,作为财产或象征(AFAIK),即使它们是非标准的。

You can easily get a Uint8Array view of your buffer the standard way like this though: 您可以通过这样的标准方式轻松获取缓冲区的Uint8Array视图:

new Uint8Array(myBuffer)

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 您首先需要将数组缓冲区转换为类型化数组,然后从那里您可以使用spread运算符来创建数组

const typedArray = new Uint8Array(myBuffer);
const array = [...typedArray];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM