请帮助我如何在 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]};
我想基于ID来迭代上面的那个..... 100,120,125 ............
答案 0 :(得分: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);
});