[英]How can I make an array of arrays of custom objects in Javascript?
So, I have a friendly neighborhood object constructor, like so; 因此,我有一个友好的邻域对象构造函数,就像这样;
function Clip(a, b)
{
this.track = a
this.slot = b
this.path = "live_set tracks " + a + " clip_slots " + b + " clip "
clip = new LiveAPI(this.patcher, this.path)
this.length = clip.get("length")
}
What I'd like to do is 我想做的是
In other words, the superarray should allow me to access the objects' properties and methods by, for instance, clip[0][0].length - clip[0][7].length
, clip[1][0].length - clip[1][7].length
, etc. 换句话说,超级数组应该允许我通过例如
clip[0][0].length - clip[0][7].length
, clip[1][0].length - clip[1][7].length
访问对象的属性和方法clip[1][0].length - clip[1][7].length
等
Is this what you're looking for? 这是您要找的东西吗? I simplified some of the code, but the general idea seems to fit.
我简化了一些代码,但总体思路似乎合适。
http://jsfiddle.net/bryandowning/pH6bU/ http://jsfiddle.net/bryandowning/pH6bU/
var superArr = [];
function Clip(a) {
this.length = a;
}
/*
* num: number of clip collections to add to container
* max: number of clips per collection
* container: array to add collections to
*/
function addClips( num, max, container ){
while(num--){
// arr: a collection of clips
var arr = [];
for( var i = 0; i < max; i++ ){
arr.push(
// just did a random number for the length
new Clip( Math.floor( Math.random() * 10 ) )
);
}
container.push( arr );
}
}
addClips( 5, 8, superArr );
console.log( superArr );
console.log( superArr[0][0].length );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.