[
{
"id": 1,
"name": "sys",
"title": "系统设置",
"type": 1,
"status": 1,
"condition": "",
"pid": 0,
"level": 0,
"sort": 7,
"icon": "fa-gear",
"children": [
{
"id": 11,
"name": "conf/lst",
"title": "配置列表",
"type": 1,
"status": 1,
"condition": "",
"pid": 1,
"level": 1,
"sort": 50,
"icon": null,
"children": [
{
"id": 12,
"name": "conf/add",
"title": "添加配置",
"type": 1,
"status": 1,
"condition": "",
"pid": 11,
"level": 2,
"sort": 50,
"icon": null,
"children": []
},
{
"id": 13,
"name": "conf/del",
"title": "配置删除",
"type": 1,
"status": 1,
"condition": "",
"pid": 11,
"level": 2,
"sort": 50,
"icon": null,
"children": []
},
{
"id": 14,
"name": "conf/edit",
"title": "配置编辑",
"type": 1,
"status": 1,
"condition": "",
"pid": 11,
"level": 2,
"sort": 50,
"icon": null,
"children": []
}
]
},
{
"id": 9,
"name": "conf/conf",
"title": "配置项",
"type": 1,
"status": 1,
"condition": "",
"pid": 1,
"level": 1,
"sort": 50,
"icon": null,
"children": []
}
]
},
{
"id": 15,
"name": "admin",
"title": "管理员",
"type": 1,
"status": 1,
"condition": "",
"pid": 0,
"level": 0,
"sort": 50,
"icon": "fa-user",
"children": [
{
"id": 16,
"name": "admin/lst",
"title": "管理员列表",
"type": 1,
"status": 1,
"condition": "",
"pid": 15,
"level": 1,
"sort": 50,
"icon": null,
},
{
"id": 27,
"name": "authrule/lst",
"title": "权限列表",
"type": 1,
"status": 1,
"condition": "",
"pid": 15,
"level": 1,
"sort": 50,
"icon": null,
},
{
"id": 30,
"name": "authgroup/lst",
"title": "用户组",
"type": 1,
"status": 1,
"condition": "",
"pid": 15,
"level": 1,
"sort": 50,
"icon": null,
}
]
}
]
上面的json是多維數組,我想用js for循環把children下面的數組輸出,但不知道為什麼輸出不了,也沒報錯.
$.ajax({
type: "get",
url: "/admin/index/menu",
async: true,
dataType: 'json',
success: function(res) {
for(var i = 0; i < res.length; i++) {
console.log(res[i].children); //这个能输出
for (var a=0;a<res[i].children;a++) {
console.log(res[i].children[a]); //这个不能输出,也没有报错
}
}
}
})
請問是哪裡錯了?
雷雷
a ->
a
雖然來晚了,但我覺得還是可以補充一下
一般我個人比較喜歡使用
foreach
遍歷,在 JS 裡是(以此例中的程式碼為例)上面用了es6的箭頭函數,如果要在 es5 中寫,直接換成 function 表達式就好
這裡應該要做個遞歸,推薦了解下遞歸知識
遞歸遍歷節點