av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

javascript - js 多維數組的問題

瀏覽:202日期:2023-04-06 16:04:59

問題描述

[ {'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]); //這個不能輸出,也沒有報錯 }} }})

請問是哪里錯了?

問題解答

回答1:

$.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.length; a++) { // <-- 此處少了.length,數字和對象比較大小,結果為false,第二個條件一次也滿足不了console.log(res[i].children[a]); }} }}回答2:

a<res[i].children -> a<res[i].children.length

回答3:

雖然來晚了,但是我覺得還是可以補充一下

一般我個人比較喜歡使用 foreach 遍歷,在 JS 里是(以此例中的代碼為例)

res.forEach(r => { r.children.forEach(c => {// do something });});

上面用了es6的箭頭函數,如果要在 es5 中寫,直接換成 function 表達式就好

回答4:

這里應該是要做個遞歸,推薦了解下遞歸知識遞歸遍歷節點

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 国产欧美日韩一区二区三区 | 岛国视频| 北条麻妃av一区二区三区 | 免费亚洲一区二区 | 人人九九精 | 伊人二区| 午夜在线视频一区二区三区 | 在线一区 | 91精品国产91久久久久青草 | 国产精品一级 | 国产精品久久久久久高潮 | 欧美精品乱码久久久久久按摩 | 久久久影院| 亚洲综合电影 | 久久久久亚洲精品中文字幕 | 操操操av| www.天天操 | 日韩乱码av | 精品欧美一区二区三区免费观看 | 天天碰夜夜操 | 久久久精品天堂 | 国产精品美女久久久 | 精品区| 国产精品一区二 | 国产日韩欧美一区二区 | 国产精品久久久久久久久久三级 | 国产xxxx岁13xxxxhd | 亚洲视频精品 | 在线看无码的免费网站 | 天堂va在线观看 | 第一区在线观看免费国语入口 | 国产精品一区久久久 | 91啪影院 | 亚洲毛片在线观看 | 91免费观看国产 | 岛国视频| 一级毛片免费视频观看 | 成人午夜免费视频 | 成人在线免费视频观看 | 天堂一区二区三区四区 | 久久aⅴ乱码一区二区三区 91综合网 |