如何在java脚本

时间:2018-04-06 06:45:05

标签: javascript

以下是正确的方法来定义下面的对象数组是我的javascript代码,谢谢

var A1 = [
      B1 : [
             { Text: 'Home',
               name: "i do not know"
              },
              { Text: 'Taj',
                name: "you may know"
              }
           ],
      B2 : [
             { Text: 'Homeone',
              name: "i do not knows"
               },
              { Text: 'Tasj',
              name: "you may knows"
             }
           ]
          ]

3 个答案:

答案 0 :(得分:1)

var A1 = [
  {B1 : [
         { Text: 'Home',
           name: "i do not know"
          },
          { Text: 'Taj',
            name: "you may know"
          }
       ]},
  {B2 : [
         { Text: 'Homeone',
          name: "i do not knows"
           },
          { Text: 'Tasj',
          name: "you may knows"
         }
       ]}
      ];

请参阅代码。您应该使用object声明您的数组作为其属性。

答案 1 :(得分:0)

可以通过以下方式定义Array数组:

var A1 = [[
    {
        Text: 'Home',
        name: 'i do not know'
    },
    {
         Text: 'Taj',
         name: 'you may know'
    }
 ], [
    {
        Text: 'Homeone',
        name: 'i do not knows'
    },
    {
        Text: 'Tasj',
        name: 'you may knows'
    }
 ]]

答案 2 :(得分:0)

使用数组文字定义的简单方法如下:

var myArray = [
                 [{
                     Text:'Home',
                     name: "i do not know"
                   },
                  {
                    Text: 'Taj',
                    name: "you may know"
                  }],
                 [{
                    Text: 'Homeone',
                    name: "i do not knows"
                  },
                  {
                    Text: 'Tasj',
                    name: "you may knows"
                  }]
               ];

或者根据您的代码,您可以采取其他方式,如下所示:

定义/获取每个对象并将其传递给数组

例如:

var B1 =  [
         { Text: 'Home',
           name: "i do not know"
          },
          { Text: 'Taj',
            name: "you may know"
          }
       ];
  var B2 = [
         { Text: 'Homeone',
          name: "i do not knows"
           },
          { Text: 'Tasj',
          name: "you may knows"
         }
       ];

   var  A1 = [ B1, B2 ];