-2

请帮助我如何在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迭代上面的一个.....100,120,125............

4

1 回答 1

4

首先,检查您的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);
});
于 2012-10-01T09:45:20.367 回答