2012-10-01 70 views
-3

請幫助我如何在jquery中迭代以下內容。如何在jquery函數中迭代包含列表的地圖

var data={100=[submenu 4, submenu 5, submenu 6], 
      120=[submenu 10, submenu 11, submenu 12], 
      125=[submenu 19, submenu 20, submenu 21], 
      99=[submenu 16, submenu 17, submenu 18], 
      77=[submenu 13, submenu 14, submenu 15], 
      70=[submenu 1, submenu 2, submenu 3], 
      44=[submenu 7, submenu 8, submenu 9]}; 

我想迭代基礎上,IDS上述一個..... 100120125 ............

+0

你應該先解決你的問題。 – undefined

回答

2

首先,檢查你的data對象是正確的:

var data = { "100" : ["submenu 4", "submenu 5", "submenu 6"], 
      "120" : ["submenu 10", "submenu 11", "submenu 12"], 
      "125" : ["submenu 19", "submenu 20", "submenu 21"], 
      "99" : ["submenu 16", "submenu 17", "submenu 18"], 
      "77" : ["submenu 13", "submenu 14", "submenu 15"], 
      "70" : ["submenu 1", "submenu 2", "submenu 3"], 
      "44" : ["submenu 7", "submenu 8", "submenu 9"] }; 

然後你可以使用for循環迭代對象中的元素:

for (var key in data) { 
    console.log(data[key]); 
} 

您可能會在同樣使用jQuery $.each()方法:

$.each(data, function(key, value) { 
    console.log(key); 
}); 
+1

然後你的代碼是錯誤的。沒有辦法在Javascript中運行語法錯誤的代碼,所以你永遠無法解析這個。 – devnull69

+0

感謝您的回覆,但我從spring mvc模型屬性中獲取了這些數據。我的實際代碼是var data ='$ {test1}';那我該如何改變呢? –