簡體   English   中英

如何在Javascript中創建自定義對象數組的數組?

[英]How can I make an array of arrays of custom objects in 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訪問對象的屬性和方法clip[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 );


​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM