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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

javascript - js 多維數(shù)組的問(wèn)題

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

問(wèn)題描述

[ {'id': 1,'name': 'sys','title': '系統(tǒng)設(shè)置','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': '配置項(xiàng)','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': '權(quán)限列表','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是多維數(shù)組,我想用js for循環(huán)把children下面的數(shù)組輸出,但不知道為什么輸出不了,也沒(méi)報(bào)錯(cuò).

$.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); //這個(gè)能輸出 for (var a=0;a<res[i].children;a++) {console.log(res[i].children[a]); //這個(gè)不能輸出,也沒(méi)有報(bào)錯(cuò) }} }})

請(qǐng)問(wèn)是哪里錯(cuò)了?

問(wèn)題解答

回答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,數(shù)字和對(duì)象比較大小,結(jié)果為false,第二個(gè)條件一次也滿足不了console.log(res[i].children[a]); }} }}回答2:

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

回答3:

雖然來(lái)晚了,但是我覺(jué)得還是可以補(bǔ)充一下

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

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

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

回答4:

這里應(yīng)該是要做個(gè)遞歸,推薦了解下遞歸知識(shí)遞歸遍歷節(jié)點(diǎn)

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 中文在线免费观看 | 免费观看一区二区三区毛片 | 亚洲砖区区免费 | 久久不卡视频 | 狠狠干狠狠干 | 蜜桃av一区 | av黄色在线观看 | 国产999久久久 | 狠狠五月| 波多野结衣一级 | 日韩欧美一区在线 | 亚洲成人精品视频 | 美女黄色在线观看 | 久草青青草 | 色婷婷久久 | 伊人网在线播放 | 亚洲国产二区 | 亚洲一区二区在线视频 | 91视频在线观看视频 | 日韩精品网站 | 亚洲无av在线中文字幕 | 亚洲成人精品视频 | 一区二区在线看 | 一级黄色网 | 欧美成人精品欧美一级私黄 | 午夜免费视频 | 亚洲精品资源 | 亚洲在线视频 | 九月丁香婷婷 | 国产三区四区 | 1024国产精品 | 成年女人毛片 | 国语对白做受欧美 | 中文字幕在线视频观看 | 一级毛片黄色 | 国产在线观看精品 | 天天久久综合 | 精品一二区 | 五月天婷婷基地 | 成人激情视频在线观看 | 六月丁香激情 |