Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:Array.prototype方法在32个多维数组上工作吗_Javascript_Arrays - Fatal编程技术网

Javascript:Array.prototype方法在32个多维数组上工作吗

Javascript:Array.prototype方法在32个多维数组上工作吗,javascript,arrays,Javascript,Arrays,我在Three.js中使用Float32Array作为顶点位置。我想返回一个从随机点开始的顶点位置的新数组 [0...10000].slice( n, n + 100 ) // Works positions = new Float32Array( amount * 3 ) randPositions = positions.slice( n, n + 100 ) // Doesn't work - undefined is not a function 但是,当我这样做

我在
Three.js
中使用
Float32Array
作为顶点位置。我想返回一个从随机点开始的顶点位置的新数组

[0...10000].slice( n, n + 100 ) // Works 

positions      = new Float32Array( amount * 3 ) 
randPositions  = positions.slice( n, n + 100 )  // Doesn't work - undefined is not a function 

但是,当我这样做时,它会返回一个错误(
位置
已定义且有数据)?是
Array.prototype
方法与
Float32Array
兼容吗?

许多看起来像数组的东西-它们具有
.length
属性和数字索引属性-使用内置数组方法,但必须显式调用它们:

randPositions = [].slice.call(positions, n, n + 100);

这个技巧从一个丢弃的数组实例中获取对“slice”方法的引用,并通过
.call()
调用它,使您的“positions”数组成为函数中的
这个

尝试
[].slice.call(positions,n,n+100)(还没试过,可能不行)@Pointy,我来试试。谢谢,是的!这很有效。加上这个作为答案,我会认为它是正确的。非常聪明和优雅。我很谦卑。@ForeignObject:您应该知道结果不是类型化数组。根据您的需要,您最好只使用您需要的范围,可能通过包装对象提供方便的方法。或者,如果您需要子集的实际副本,则可能需要手动复制数据。@Cookie Monster是的,我在键入此内容后也意识到了这一点,但我已关闭笔记本电脑并开始驾车穿越城镇:)