Gecko 2 中的类型化数组:Float32Array 串联和扩展
我对 Javascript 类型数组有点困惑。
我有几个 Float32Array ,它们没有 concat 方法。顺便说一句,我不知道他们提前有多少。 我想将它们全部连接到另一个 Float32Array 中,但是:
- 正如我之前所说,如果我尝试写入超过数组长度,则没有连接方法
- ,该数组不会扩展(又名这不起作用 - 请注意event.frameBuffer 和 buffer 都是 Float32Array,我不知道缓冲区的最终长度是多少):
var length_now = buffer.length;
for (var i = 0; i < event.frameBuffer.length; i += 1) {
buffer [length_now + i] = event.frameBuffer[i];
}
我找到的唯一解决方案是将 Float32Array 复制到常规数组中,这绝对不是我想要的。你会怎么做,stackoverflowers?
I'm a bit confused with Javascript Typed Arrays.
What I have are several Float32Array s, that have no concat method. I don't know how many are them in advance, btw.
I'd like to concatenate them all inside another Float32Array, but:
- as I said before, there is no concatenation method
- if I try to write past the array length, the array is not expanded (aka this won't work - please note that event.frameBuffer and buffer are both Float32Array and that I don't know what the final length of my buffer will be):
var length_now = buffer.length;
for (var i = 0; i < event.frameBuffer.length; i += 1) {
buffer [length_now + i] = event.frameBuffer[i];
}
The only solution I found is to copy the Float32Array in a regular array, that's definitely not what I want. How would you do, stackoverflowers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类型化数组基于数组缓冲区,无法动态调整大小,因此无法写入数组末尾或使用
push()
。实现您想要的效果的一种方法是分配一个新的 Float32Array ,它足够大以包含两个数组,并执行优化的复制:
这将允许您编写:
Typed arrays are based on array buffers, which cannot be resized dynamically, so writing past the end of the array or using
push()
is not possible.One way to achieve what you want would be to allocate a new
Float32Array
, large enough to contain both arrays, and perform an optimized copy:That would allow you to write:
或者,如果您尝试加入 N 个数组:
Or if you're trying to join N arrays:
我遇到了同样的问题,您现在可以将以下内容添加到原型中,
您可以简单地执行以下操作
I had the same issue, you can add the following to the prototype
now you can simply do