javascript - 如何在 Javascript 中创建自定义对象数组的数组?

标签 javascript

所以,我有一个友好的邻居对象构造函数,就像这样;

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")
}

我想做的是

  1. 将任意数量的它们添加到数组中
  2. 当数组的长度达到 8 时,将该数组添加到新的“ super ”数组并开始一个新数组。

换句话说,超数组应该允许我访问对象的属性和方法,例如,clip[0][0].length - Clip[0][7].lengthclip[1][0].length - Clip[1][7].length

最佳答案

这是您要找的吗?我简化了一些代码,但总体思路似乎是合适的。

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 );


​

关于javascript - 如何在 Javascript 中创建自定义对象数组的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277890/

相关文章:

javascript - 仅当存在另一个类时才添加类

javascript - Flexslider导航控件大小

javascript - 删除嵌套的 json 元素并显示为单独的键值对

javascript - 如何阅读代码中的函数描述?

javascript - 我可以使用类似 javascript 的东西组合两个 html 按钮及其 onclick 方法吗?

javascript - 无法在 Firefox 中设置默认焦点

javascript - 如何在 vue-router 中使用 Vuetify 选项卡

javascript - 用户界面路由器 : default route based on user role

javascript - HTTP 响应中的意外内容

javascript - 隐藏具有特定属性的元素