2011-04-27 50 views
0

我需要动态创建一个数组,但我真的不能找到一个解决办法...如何创建一个JavaScript多维数组

基本上是我需要的:一个类型和项目的数量挂钩的ID在里面。 然后为每个ID我需要添加一个可变数量的项目。

所以最终的例子必须是这样的:

id : 59 | type : combo_box | NbItem : 1 
Item 1 
name : text | value : test 
name : icon | value : test.png 

id : 60 | type : search_box | NbItem : 2 
Item 1 
name : text | value : Yahoo 
name : icon | value : yahoo.png 
name : weblink | value : yahoo.com 

Item 2 
name : text | value : Bing 
name : icon | value : Bing.png 
name : weblink | value : Bing.com 

我再次,它有精确的是动态的。我需要在执行过程中进行添加,像array[60][name][0] = text

编辑

我想继续这样,但它失败:

var dropMenuArray; 

var node = XMLDoc.getElementsByTagName("item")[itemCpt].getElementsByTagName("m_type")[0]; 
type = node.childNodes[0].nodeValue; 

node = XMLDoc.getElementsByTagName("item")[itemCpt].getElementsByTagName("m_id")[XMLDoc.getElementsByTagName("item")[itemCpt].getElementsByTagName("m_id").length-1]; 
id = node.childNodes[0].nodeValue; 

if ((type.indexOf('combo_button') != -1 && type.indexOf('combo_button_item') == -1) || type.indexOf('search_box') != -1) { 
    dropMenuArray[id] = { 
     Type: type, 
     items: [] 
    }; 

    alert('Index : ' + id + ' - Type : ' + type); 
} 

我的意思是没有警报,当我把在commantary上创建数组我有警报弹出窗口。

+1

到目前为止,您到底尝试过什么? – 2011-04-27 15:09:24

+0

您应该将每个组存储在一个对象中,然后拥有这些对象的数组。 – musaul 2011-04-27 15:14:31

+0

+1给Musual。 如果你曾经用PHP编码过,你必须知道有两种类型的数组:索引和键值。在JavaScript中,键值数组称为对象。 而当你使用[名字]它可能是你想要的。 – xavierm02 2011-04-27 15:24:36

回答

0

你可以做这样的事情(使用对象):

var array = { 
    59: { 
     type: 'combo_box', 
     NbItem: 1, 
     name: ['text', 'test', 'icon'] 
    }, 
    60: { 
     type: 'search_box', 
     NbItem: 2, 
     name: ['text', 'yahoo', 'weblink'] 
    }, 
} 

//can also use array[60]['name'][1] below: 
alert(array[60].name[1]); // will alert 'yahoo' 
array[60].name[1] = 'google'; 
alert(array[60].name[1]); // will alert 'google' 
1

我想你想要的是一个这样的数组:

var multi = [ { type: "combo_box", items: [ { name: "text", value: "Yahoo" } ] } ]; 

因此,要添加新条目,你会更多信息:

var multi[newIndex] = { type: "new type", items: [] }; 

要将项目添加到一个:

multi[newIndex].items.push({ name: "text", value: "Bing" }); 

由于JavaScript会为您保留“items”列表的“length”属性,因此您并不需要明确存储项目数。因此,

var howMany = multi[someIndex].items.length; 
+0

感谢这似乎是我所为,我会试试这个! – Sindar 2011-04-27 15:38:40

+0

不要真的工作,看我的编辑PLZ;) – Sindar 2011-04-27 16:03:26

+0

那么你可能想要“警告()”“类型”的价值是什么。 – Pointy 2011-04-27 16:10:27

0

您可以使用关联数组:

function Item(text,icon,weblink) { 
this.text = text; 
this.icon = icon; 
this.weblink = weblink; 
} 

var arr = new Array(); 

var a = new Object(); 
a["id"] = 59; 
a["type"] = "combo_box"; 
var arr_items = new Array(); 
arr_items.push(new Item("test","test.png")); 
arr_items.push(new Item("Yahoo", "yahoo.png", "yahoo.com")); 
a["items"] = arr_items; 

arr.push(a); 

... //carry on with other objects 
+0

上面的代码值得注意的不是JSON - 不确定是否可以在JSON中执行任何数量的属性。 – Liv 2011-04-27 15:14:55

0

只要把阵列,阵列... 但你可能会喜欢的对象。

这是你描述的结构,但我认为有更好的结构。

[ 
    { 
     "id": 59, 
     "type": "combo_box", 
     "items": [ 
      [ 
       { 
        "name": "text", 
        "value": "test" 
       }, 
       { 
        "name": "icon", 
        "value": "test.png" 
       } 
      ] 
     ] 
    }, 
    { 
     "id": 60, 
     "type": "search_box", 
     "items": [ 
      [ 
       { 
        "name": "text", 
        "value": "Yahoo" 
       }, 
       { 
        "name": "icon", 
        "value": "yahoo.png" 
       }, 
       { 
        "name": "weblink", 
        "value": "yahoo.com" 
       } 
      ], 
      [ 
       { 
        "name": "text", 
        "value": "Bing" 
       }, 
       { 
        "name": "icon", 
        "value": "Bing.png" 
       }, 
       { 
        "name": "weblink", 
        "value": "Bing.com" 
       } 
      ] 
     ] 
    }, 
] 

NBItem事情可以通过获取item数组的length属性来获得。

这是较短的方式,可能会更好:

[ 
    { 
     "id": 59, 
     "type": "combo_box", 
     "items": [ 
      { 
       "text": "test", 
       "icon": "test.png" 
      } 
     ] 
    }, 
    { 
     "id": 60, 
     "type": "search_box", 
     "items": [ 
      { 
       "text": "Yahoo", 
       "icon": "yahoo.png", 
       "weblink": "yahoo.com" 
      }, 
      { 
       "text": "Bing", 
       "icon": "Bing.png", 
       "weblink": "Bing.com" 
      } 
     ] 
    }, 
] 

你可以做一个最后的变化,如果这些元素的ID是他的索引数组中,你可以只是把它拿走,因为当你将遍历数组,你将已经有一个包含它的变量。

+0

不错,这是我想要的结构,但在这里,任何事情都是动态的家伙... – Sindar 2011-04-27 15:39:25

+0

那么,如果你存储该对象,你可以很容易地更改属性... – xavierm02 2011-04-27 15:54:51

+0

我编辑了我的文章 – Sindar 2011-04-27 15:57:08