简体   繁体   中英

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

  1. Add an an arbitrary number of them to an array
  2. When the length of the array hits 8, add that array to a new "super"array and start a new array.

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.

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/

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


​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM