【问题标题】:Javascript two dimensional object arrayJavascript二维对象数组
【发布时间】:2015-01-07 12:36:40
【问题描述】:

我正在尝试定义对象数组,我可以定义对象的一维数组,但是当我尝试定义二维时,我得到了一个错误。在Javascript中定义多维对象数组的正确方法是什么?这是我的代码:

for(var i=0;i<3;i++)
{
   obj1[i] = [
      {property1},{property2}
   ];
   for(var j=0;j<2;j++)
   {
      obj2[i][j]= [
         {property1},{property2}
      ];
   }
}

【问题讨论】:

  • 我在 google 上搜索了你的问题,它在 1/3 秒内给了我 265,000 个可行的结果。
  • 首先了解如何使用来自@Johnny Henly 的链接在javascript 中创建二维数组,然后在dyn-web.com/tutorials/object-literal 学习如何使用对象表示法。您的语法不正确,您需要为您定义的属性提供值

标签: javascript arrays object multidimensional-array javascript-objects


【解决方案1】:

我想你想要:

for (i=0;i<3;i++) {
 f[i]=new Array();
 for (j=0;j<2;j++) {
  f[i][j] = appropriate property ;
 }
}

【讨论】:

    【解决方案2】:

    感谢所有帮助,答案是这样使用它:

    var obj1 = new Array();
    var obj2 = new Array();
    for(var i=0;i<3;i++)
    {
       obj1[i] = [
          {property1},{property2}
       ];
       var obj2[i] = new Array();
       for(var j=0;j<2;j++)
       {
          obj2[i][j]= [
             {property1},{property2}
          ];
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 2021-06-16
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多