简体   繁体   English

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

[英]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. 当数组的长度达到8时,将该数组添加到新的“ super”数组中并启动一个新数组。

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].lengthclip[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.

相关问题 JavaScript:如何合并这两个不完整对象的 arrays 并制作完整对象的数组 - JavaScript: how can I merge these two arrays of incomplete objects and make an array of complete objects 如何合并两个数组并使它们成为 JavaScript 中的对象数组 - How can I merge two arrays and make them array of objects in JavaScript 如何在 JavaScript 中解构数组数组? - How can I destructure an array of arrays in JavaScript? 如何在Javascript中使用forEach方法或数组对象的任何数组方法? - How can I use a forEach method for or any array methods for Objects of arrays in Javascript? 如何将数组数组转换为对象数组 - How can I convert an array of arrays into an array of objects 在Javascript中,如何制作具有可实例化的自定义行为的数组? - In Javascript how to make arrays with custom behavior that can be instantiated? 如何在 myArray 的 Javascript 中创建一个包含嵌套数组的数组? - How can I make an array instaed of nested arrays in Javascript from myArray? 如何从初始数组中制作 2 个不同的改组 arrays? javascript - How can i make 2 different shuffled arrays from an initial array? javascript 如何将javascript动态数组转换为具有自定义键名的多个对象的数组 - How can i convert a javascript dynamic array to an array of multiple objects with custom key names 如何在JavaScript中将带有JSON对象的二维数组转换为单个数组? - How can I make two dimensional array with JSON objects into single array in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM