【问题标题】:array of objects containing arrays possible?包含数组的对象数组可能吗?
【发布时间】:2010-11-27 16:51:24
【问题描述】:

如果选中页面上的特定复选框(每个类别一个复选框),我正在尝试在我的页面上运行幻灯片,该幻灯片从幻灯片中包含的几组(类别)图像中提取。我成功地用两个类别对此进行了编码,但我试图使代码更容易定制。我有一个绝妙的主意,即使用自定义对象而不是数组。我认为这种方法也可能让我更接近在此过程中使用 xml 文档的最终目标。

我对面向对象编程相当陌生,尤其是 javascript。我已使用以下代码成功创建了用于幻灯片页面的对象声明:

function picsobj(description,length,indivpicarray){
  this.description=description;
  this.length=length;
  this.indivpicarray=indivpicarray;
}

在其他地方使用以下代码制作 picsobj 对象数组

for(i=0;i<2;i++){ //change i< integer to number of picture arrays
  picarrays[i]=new picsobj();}

计划是使用 description 属性作为标题或描述页面上的元素,使用 length 属性来帮助确定要循环浏览的图片数量,并使用(这是我的问题所在......)名为 indivpicarray 的对象属性来存储图像名称的数组(数组的长度将从 picsobj 更改为 picsobj)。我不知道这是否可行,如果可行,我需要语法方面的帮助。感谢您阅读我的问题。再次,对不起,如果有任何误用的词我有点n00b并且通过“查看源代码,复制,粘贴,更改”学到了很多

【问题讨论】:

  • 我删除了您对成为“n00b”的道歉,因为这是一个措辞恰当且有效的问题。 :)

标签: javascript arrays object


【解决方案1】:

并使用名为的对象属性 indivpicarray 存储一个数组 图片名称。

这很好。具有数组属性的对象的技术非常普遍。数组的处理方式与其他任何变量一样。只需将其添加到您的构造函数中:

function picsobj(description,length,indivpicarray){
  this.description=description;
  this.length=length;
  this.indivpicarray=indivpicarray;
}

imageNameArray = [ "image1.png", "image2.gif", "image3.jpg" ];
var myPicsObj = new picsobj( "this is the description", 3, imageNameArray );

see here if you're not sure what a constructor is

【讨论】:

    【解决方案2】:

    在 javascript 中一切皆有可能:

    this.description = ["a","b","c"];
    

    您将拥有一个由 3 个项目组成的数组,它们是字符串。在现实世界中,您会将其定义为 this.description = []; 然后做类似的事情:

    picarrays[i].description.push("a");
    picarrays[i].description.push("b");
    picarrays[i].description.push("c");
    

    有效地为您提供相同的数组,您可以遍历 .description 以获取数组中的所有字符串。您也可能不需要长度,因为 description.length 会给您描述中的项目数。

    【讨论】:

      【解决方案3】:

      是的,这是可能的。例如,如果您在编程时已经知道值,则可以这样做:

      var picarrays=[{description:"bla","length":1234,"indivpicarray":["a","b","c"]},
                     {description:"blah","length":145,"indivpicarray":["a","c","g"]}];
      

      这给你一个数组对象的数组。

      【讨论】:

        【解决方案4】:

        在 JavaScript 中有一个名为 Array 的对象,它应该完全满足您的需求。 请参阅http://www.w3schools.com/JS/js_obj_array.asp 上的示例,或者只是谷歌“javascript Array”

        希望有帮助

        【讨论】:

        • 发帖人已经知道了。他正在寻找一种将用户定义的对象插入/推送到数组中的方法,而不是内置的简单类型。
        【解决方案5】:
        /* The function constructor of your objects usually use a capitalized names 
         * */
        var Picsobj = function(description,indivpicarray){
          this.description=description;
          this.indivpicarray=indivpicarray;
        };
        /* Is better convert length to a method instead a property passed to the constructor. */ 
        /* The methods are attached to the prototype propertie of the constructor. `this` refers to 
         * the actual object where the method is called.
         *  */ 
        Picsobj.prototype.getLength = function() {
          if(typeof(this.indivpicarray) != 'object')
            return 0;
          return this.indivpicarray.length;
        };
        
        
        /* Example */
        var pic = new Picsobj('Description of pic', ['pic1.jpg','pic2.jpg']);
        var anotherPic = new Picsobj('Description of anotherPic', ['anotherPic1.jpg','anotherPic2.jpg']);
        console.log(pic.getLength());
        console.log(anotherPic.getLength());
        

        【讨论】:

          猜你喜欢
          • 2014-03-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-15
          • 2019-07-30
          • 1970-01-01
          • 1970-01-01
          • 2019-06-06
          • 1970-01-01
          相关资源
          最近更新 更多