js實(shí)現(xiàn)無(wú)限層級(jí)樹(shù)形數(shù)據(jù)結(jié)構(gòu)(創(chuàng)新算法)
由于做項(xiàng)目的需要,把一個(gè)線性數(shù)組轉(zhuǎn)成樹(shù)形數(shù)組,在網(wǎng)上查了很多文章,覺(jué)得他們寫(xiě)的太復(fù)雜了,于是自己寫(xiě)了一個(gè),在折騰了一下午終于把它寫(xiě)出來(lái)啦(激動(dòng).gif),用兩個(gè)filter過(guò)濾器就搞定了,代碼簡(jiǎn)潔明了,數(shù)據(jù)結(jié)構(gòu)小白都能看懂。
js代碼:把扁平數(shù)據(jù)轉(zhuǎn)成樹(shù)形數(shù)據(jù)
function setTreeData(source){
let cloneData = JSON.parse(JSON.stringify(source)) // 對(duì)源數(shù)據(jù)深度克隆
return cloneData.filter(father=>{ // 循環(huán)所有項(xiàng),并添加children屬性
let branchArr = cloneData.filter(child=> father.id == child.parentId); // 返回每一項(xiàng)的子級(jí)數(shù)組
branchArr.length>0 ? father.children=branchArr : '' //給父級(jí)添加一個(gè)children屬性,并賦值
return father.parentId==0; //返回第一層
});
}
根據(jù)網(wǎng)友給我指出的問(wèn)題,之前的算法會(huì)影響到源數(shù)據(jù),之后我對(duì)獲取的數(shù)據(jù)進(jìn)行了深度克隆,完美解決。
封裝函數(shù):
function treeData(source, id, parentId, children){
let cloneData = JSON.parse(JSON.stringify(source))
return cloneData.filter(father=>{
let branchArr = cloneData.filter(child => father[id] == child[parentId]);
branchArr.length>0 ? father[children] = branchArr : ''
return father[parentId] == 0 // 如果第一層不是parentId=0,請(qǐng)自行修改
})
}
// 調(diào)用時(shí),字段名以字符串的形式傳參,如treeData(source, 'id', 'parentId', 'children')
實(shí)例1:使用element-ui的組件制作一個(gè)樹(shù)形多級(jí)嵌套伸縮菜單欄
實(shí)現(xiàn)效果:

vue組件:
<template>
<el-tree
:data="treeData"
:props="defaultProps"
accordion
@node-click="handleNodeClick">
</el-tree>
</template>
<script>
export default {
name: "Test",
data(){
return {
data : [
{id:1,parentId:0,name:"一級(jí)菜單A",rank:1},
{id:2,parentId:0,name:"一級(jí)菜單B",rank:1},
{id:3,parentId:0,name:"一級(jí)菜單C",rank:1},
{id:4,parentId:1,name:"二級(jí)菜單A-A",rank:2},
{id:5,parentId:1,name:"二級(jí)菜單A-B",rank:2},
{id:6,parentId:2,name:"二級(jí)菜單B-A",rank:2},
{id:7,parentId:4,name:"三級(jí)菜單A-A-A",rank:3},
{id:8,parentId:7,name:"四級(jí)菜單A-A-A-A",rank:4},
{id:9,parentId:8,name:"五級(jí)菜單A-A-A-A-A",rank:5},
{id:10,parentId:9,name:"六級(jí)菜單A-A-A-A-A-A",rank:6},
{id:11,parentId:10,name:"七級(jí)菜單A-A-A-A-A-A-A",rank:7},
{id:12,parentId:11,name:"八級(jí)菜單A-A-A-A-A-A-A-A",rank:8},
{id:13,parentId:12,name:"九級(jí)菜單A-A-A-A-A-A-A-A-A",rank:9},
{id:14,parentId:13,name:"十級(jí)菜單A-A-A-A-A-A-A-A-A-A",rank:10},
],
defaultProps: {
children: 'children',
label: 'name'
}
}
},
computed:{
treeData(){
let cloneData = JSON.parse(JSON.stringify(this.data)) // 對(duì)源數(shù)據(jù)深度克隆
return cloneData.filter(father=>{
let branchArr = cloneData.filter(child=>father.id == child.parentId) //返回每一項(xiàng)的子級(jí)數(shù)組
branchArr.length>0 ? father.children = branchArr : '' //如果存在子級(jí),則給父級(jí)添加一個(gè)children屬性,并賦值
return father.parentId==0; //返回第一層
});
}
},
methods:{
handleNodeClick(data){
// console.log(data)
console.log(this.treeData)
}
},
mounted(){
}
}
</script>
<style scoped>
</style>
樹(shù)形數(shù)據(jù)轉(zhuǎn)成扁平數(shù)據(jù),請(qǐng)查看這篇文章:js實(shí)現(xiàn)樹(shù)形數(shù)據(jù)轉(zhuǎn)成扁平數(shù)據(jù)
相關(guān)文章
談?wù)勎覍?duì)JavaScript原型和閉包系列理解(隨手筆記9)
這篇文章主要介紹了談?wù)勎覍?duì)JavaScript原型和閉包系列理解(隨手筆記9)的相關(guān)資料,需要的朋友可以參考下2015-12-12
IE6,IE7,IE8下使用Javascript記錄光標(biāo)選中范圍(已補(bǔ)全)
IE6,7,8下使用Javascript記錄光標(biāo)選中范圍(已補(bǔ)全)(已解決單個(gè)節(jié)點(diǎn)內(nèi)部重復(fù)字符的問(wèn)題)2011-08-08
js添加千分位的實(shí)現(xiàn)代碼(超簡(jiǎn)單)
下面小編就為大家?guī)?lái)一篇js添加千分位的實(shí)現(xiàn)代碼(超簡(jiǎn)單)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
js+html5實(shí)現(xiàn)canvas繪制橢圓形圖案的方法
這篇文章主要介紹了js+html5實(shí)現(xiàn)canvas繪制橢圓形圖案的方法,涉及html5圖形繪制的基礎(chǔ)技巧,感興趣的朋友可以參考一下2016-05-05
javascript中的數(shù)字與字符串相加實(shí)例分析
javascript中的數(shù)字與字符串相加實(shí)例分析,學(xué)習(xí)js的朋友可以參考下。2011-08-08
微信小程序 wx.login解密出現(xiàn)亂碼的問(wèn)題解決辦法
這篇文章主要介紹了微信小程序 wx.login解密出現(xiàn)亂碼的問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
javascript 回到頂部效果的實(shí)現(xiàn)代碼
本篇文章主要是對(duì)javascript 回到頂部效果的實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02

