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是的,我在键入此内容后也意识到了这一点,但我已关闭笔记本电脑并开始驾车穿越城镇:)