2011-04-27 39 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

相關問題