国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:227日期: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)文章:
主站蜘蛛池模板: 海丰县| 阜城县| 同心县| 茂名市| 阿巴嘎旗| 独山县| 虹口区| 石楼县| 黄骅市| 正镶白旗| 大厂| 昌都县| 民县| 东辽县| 隆林| 黎川县| 镇沅| 丘北县| 枞阳县| 哈密市| 郓城县| 县级市| 德庆县| 潢川县| 中方县| 延长县| 八宿县| 都匀市| 成都市| 高唐县| 玛纳斯县| 兴隆县| 阳春市| 邢台县| 永胜县| 葵青区| 石楼县| 鄂伦春自治旗| 黑山县| 叶城县| 榆中县|