<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
前端在構建國家的省市區結構時,介面返回的不是樹形結構,這個時候就需要我們進行轉化。以下資料為例
[ [ { "districtId": 1586533852834, "parentCode": "000", "nodeCode": "000001", "name": "浙江省", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533872922, "parentCode": "000001", "nodeCode": "000001001", "name": "杭州市", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533872940, "parentCode": "000001001", "nodeCode": "000001001001", "name": "上城區", "districtType": "HUADONG", "districtTypeName": null } ], [ { "districtId": 1586533852834, "parentCode": "000", "nodeCode": "000001", "name": "浙江省", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533872922, "parentCode": "000001", "nodeCode": "000001001", "name": "杭州市", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533872961, "parentCode": "000001001", "nodeCode": "000001001002", "name": "下城區", "districtType": "HUADONG", "districtTypeName": null } ], [ { "districtId": 1586533852834, "parentCode": "000", "nodeCode": "000001", "name": "浙江省", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533872922, "parentCode": "000001", "nodeCode": "000001001", "name": "杭州市", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533872980, "parentCode": "000001001", "nodeCode": "000001001003", "name": "臨安區", "districtType": "HUADONG", "districtTypeName": null } ], [ { "districtId": 1586533852834, "parentCode": "000", "nodeCode": "000001", "name": "浙江省", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533873196, "parentCode": "000001", "nodeCode": "000001002", "name": "寧波市", "districtType": "HUADONG", "districtTypeName": null } ], [ { "districtId": 1586533852834, "parentCode": "000", "nodeCode": "000001", "name": "浙江省", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533873432, "parentCode": "000001", "nodeCode": "000001003", "name": "溫州市", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533873468, "parentCode": "000001003", "nodeCode": "000001003002", "name": "平陽縣", "districtType": "HUADONG", "districtTypeName": null } ], [ { "districtId": 1586533852834, "parentCode": "000", "nodeCode": "000001", "name": "浙江省", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533873432, "parentCode": "000001", "nodeCode": "000001003", "name": "溫州市", "districtType": "HUADONG", "districtTypeName": null }, { "districtId": 1586533873486, "parentCode": "000001003", "nodeCode": "000001003003", "name": "文成縣", "districtType": "HUADONG", "districtTypeName": null } ] ]
[ { "label": "浙江省", "value": 1586533852834, "parentCode": "000", "nodeCode": "000001", "children": [ { "label": "杭州市", "value": 1586533872922, "parentCode": "000001", "nodeCode": "000001001", "children": [ { "label": "上城區", "value": 1586533872940, "parentCode": "000001001", "nodeCode": "000001001001" }, { "label": "下城區", "value": 1586533872961, "parentCode": "000001001", "nodeCode": "000001001002" }, { "label": "臨安區", "value": 1586533872980, "parentCode": "000001001", "nodeCode": "000001001003" } ] }, { "label": "寧波市", "value": 1586533873196, "parentCode": "000001", "nodeCode": "000001002" }, { "label": "溫州市", "value": 1586533873432, "parentCode": "000001", "nodeCode": "000001003", "children": [ { "label": "平陽縣", "value": 1586533873468, "parentCode": "000001003", "nodeCode": "000001003002" }, { "label": "文成縣", "value": 1586533873486, "parentCode": "000001003", "nodeCode": "000001003003" } ] } ] } ]
① 觀察輸入的資料結構為二維陣列,每個陣列中儲存了省市區的全部資料,此時首先需要將二維陣列一維化,以取得所有的節點資料,用於後續構建樹形結構。
let arr = [].concat.apply([], data)
② 得到所有需要構建樹形結構的資料後,發現陣列中存在重複資料
arrayToTree(data, rootNode) { let temp = {} let reduceArr = data.reduce((current, next) => { temp[next.districtId] ? "" : temp[next.districtId] = current.push(next) return current },[]) }
③ 資料規範化處理,把所有的資料處理成需要的節點資料
arrayToTree(data, rootNode) { let temp = {} let reduceArr = data.reduce((current, next) => { temp[next.districtId] ? "" : temp[next.districtId] = current.push(next) return current },[]) // 2.陣列規範化 let result = [] reduceArr.forEach(item => { result.push({ label:item.name, value:item.districtId, parentCode:item.parentCode, nodeCode:item.nodeCode, children: [] }) }) }
④ 採用遞迴的方式構建樹形結構
getTreeList(data, rootNode, list,{label, value,parentCode, nodeCode, children}) { // 構建父節點 data.forEach(item =>{ if (item.parentCode === rootNode) { list.push(item) } }) list.forEach(item => { item.children = [] // 構建子節點 this.getTreeList(data, item.nodeCode, item.children,{label, value,parentCode, nodeCode, children}); // 刪除空葉子節點 if (item.children.length === 0) { delete item.children; } }) return list; }
arrayToTree(data, rootNode, {label = 'label', value = 'value',parentCode = 'parentCode', nodeCode = 'nodeCode', children = ''} = {}) { // 1.陣列去重 let temp = {} let reduceArr = data.reduce((current, next) => { temp[next.districtId] ? "" : temp[next.districtId] = current.push(next) return current },[]) // 2.陣列規範化 let result = [] reduceArr.forEach(item => { result.push({ label:item.name, value:item.districtId, parentCode:item.parentCode, nodeCode:item.nodeCode, children: [] }) }) // 3.構建樹形結構 return this.getTreeList(result,rootNode,[],{ label,value,parentCode,nodeCode,children }); }, // 遞迴構建樹形結構 getTreeList(data, rootNode, list,{label, value,parentCode, nodeCode, children}) { data.forEach(item =>{ if (item.parentCode === rootNode) { list.push(item) } }) list.forEach(item => { item.children = [] this.getTreeList(data, item.nodeCode, item.children,{label, value,parentCode, nodeCode, children}); if (item.children.length === 0) { delete item.children; } }) return list; },
以上就是JS前端二維陣列生成樹形結構範例詳解的詳細內容,更多關於JS二維陣列樹形結構的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45