如何将 JavaScript 类型数组转换为 JavaScript 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12760643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a JavaScript Typed Array into a JavaScript Array
提问by stuax
I have a JavaScript Float32Array, and I would like to convert it into a regular JavaScript Array. How can I do this?
我有一个 JavaScript Float32Array,我想将它转换成一个常规的 JavaScript 数组。我怎样才能做到这一点?
回答by Anoop
回答by Cody Piersall
If you don't need to support old browsers (including IE, unfortunately), you can use Array.from
, which was added to ES6:
如果您不需要支持旧浏览器(不幸的是,包括 IE),您可以使用Array.from
,它已添加到 ES6:
var array = Array.from(floatarr);
This now worksin the new releases of every browser (except IE), and it works on all major mobile browsers too.
这现在适用于每个浏览器的新版本(IE 除外),并且也适用于所有主要的移动浏览器。
回答by Denys Séguret
You can use it as any array, which means you can do this :
您可以将它用作任何数组,这意味着您可以这样做:
var arr = [];
for (var i=0; i<myFloat32array.length; i++) arr[i] = myFloat32array[i];
But it's usually more efficient to use it as a Float32Array instead of converting it.
但通常将其用作 Float32Array 而不是转换它更有效。
If you don't want to mix different types of values, don't convert it.
如果您不想混合不同类型的值,请不要转换它。