vue樹形結(jié)構(gòu)數(shù)據(jù)處理的方法總結(jié)
- 將樹形節(jié)點(diǎn)改為一維數(shù)組
const generateList = (data: any, dataList: any[] = []) => { // console.log(data,dataList, 183); for (let i = 0; i < data?.length; i++) { const node = data[i]; const { id } = node; dataList?.push(id); if (node.children) { generateList(node.children, dataList); } } return dataList }
data 為要處理的樹形結(jié)構(gòu)數(shù)組, dataList 為空數(shù)組,循環(huán)遍歷 data 數(shù)組,取到節(jié)點(diǎn) id,push 到 dataList 中,得到新數(shù)組,如果有子集就繼續(xù)遍歷取節(jié)點(diǎn),這樣遞歸下去就可以得到所有節(jié)點(diǎn)的一維數(shù)組。
- 通過節(jié)點(diǎn),查找該節(jié)點(diǎn)在樹形結(jié)構(gòu)中的路徑
const getPathByKey = (curKey: any[], data: departTree[]) => { let single: departTree[] = []; // 記錄路徑結(jié)果 let result: any = [] let traverse = (id: number, path: any, data: departTree[]) => { if (data?.length === 0) { return; } for (let item of data) { path.push(item); if (item.id === Number(id)) { single = JSON.parse(JSON.stringify(path.map((el: any) => el.id))); return; } const children = Array.isArray(item.children) ? item.children : []; traverse(id, path, children); // 遍歷子集 path.pop(); // 回溯 } } for (let i = 0; i < curKey.length; i++) { traverse(curKey[i], [], data); result.push(single) } return result; }
data 為樹形結(jié)構(gòu)數(shù)組, curKey 為節(jié)點(diǎn)數(shù)組。traverse 方法獲取節(jié)點(diǎn)路徑。循環(huán)遍歷 curKey,調(diào)用 traverse 方法就可獲得節(jié)點(diǎn)的路徑。
- 重構(gòu)統(tǒng)計(jì)樹形數(shù)據(jù),修改鍵值。
const mapTree = (org: any) => { const haveChildren = Array.isArray(org.children) && org.children.length > 0; return { label: org.egname + ' (' + org.chname + ')', value: org.id, children: haveChildren ? org.children.map((i: any) => mapTree(i)) : undefined } }
通過 map 函數(shù)遞歸出新的樹形數(shù)據(jù),定義自己想要的鍵值。
- 重新構(gòu)造樹形結(jié)構(gòu),將空子集且空員工的節(jié)點(diǎn)去掉
const generateTree = (data: any, dataList: any[] = []) => { // 將樹形數(shù)組轉(zhuǎn)變?yōu)橐痪S數(shù)組 for (let i = 0; i < data?.length; i++) { const node = data[i]; const { id, pid, name, twname, egname, employees, children } = node; dataList.push({ id, pid, name, egname, twname, employees, children }); if (node.children) { generateTree(node.children, dataList); } } // 將一維數(shù)組中的空集過濾 const array = dataList.filter((item: any) => item.employees?.length !== 0 && item.children?.length !== 0) // 將過濾后的一維數(shù)組重新生成樹形結(jié)構(gòu) return listToTree(array) } /** * 列表轉(zhuǎn)為樹,pid 值未知 * @param {*} list * @returns */ function listToTree(list: any[]) { let newArray: any[] = []; let finalTree: any[] = []; let ids: any[] = []; // id 數(shù)組 let pids: any[] = [] // pid 數(shù)組 // 獲取列表中所有的 id list.forEach((item: any) => { ids.push(item.id) }) // 獲取列表中所有的 pid list.forEach((item: any) => { pids.push(item.pid) }) // 獲取兩者差值, 篩選出 pid 不等于 id 的值,找到父節(jié)點(diǎn) pid const res = pids.filter(v => !ids.some((item) => item === v)) // 根據(jù)父節(jié)點(diǎn) pid, 找到父節(jié)點(diǎn)數(shù)據(jù) newArray = list.filter((item: any) => res.some((el) => el === item.pid)) // 構(gòu)造樹函數(shù),data 為要構(gòu)造的一維數(shù)組,parentId 為父節(jié)點(diǎn) pid function buildTree(data: any[], parentId: number) { let tree: any[] = []; data.forEach(node => { if (node.pid === parentId) { let children = buildTree(data, node.id); if (children.length > 0) { node.children = children; } tree.push(node); } }); return tree; } // 根據(jù)父節(jié)點(diǎn)數(shù)據(jù)構(gòu)造樹,考慮到父節(jié)點(diǎn) pid 會(huì)有多個(gè),循環(huán)遍歷數(shù)組。 newArray.forEach((el: any) => { finalTree.push(...buildTree(list, el.pid)) }) // 因?yàn)楦腹?jié)點(diǎn) pid 有可能有重復(fù),這里做一個(gè)去重處理 var obj = {}; finalTree = finalTree.reduce((item: any, next: any) => { obj[next.id] ? '' : obj[next.id] = true && item.push(next); return item; }, []) return finalTree }
- 將掛在子集的數(shù)據(jù)疊加掛到父級(jí)上。遞歸疊加處理
function getUsersForDepartment(departments: Department[]) { // 遞歸查找部門的所有員工 function getAllEmployees(node: Department): any[] { const employees = []; if (node?.employees) { employees.push(...node?.employees); } if (node?.children && node?.children?.length > 0) { node.children.forEach((child) => { if (getAllEmployees(child)) { employees.push(...getAllEmployees(child)); } }); } return employees; } // 將子集的數(shù)據(jù)掛載到父級(jí)的 allUsers function findSubUsers(depts: Department[]) { depts.forEach(el => { el.allUsers = getAllEmployees(el) if (el.children && el.children.length > 0) { findSubUsers(el.children) } }) } // 遞歸增加 allUsers findSubUsers(departments) }
以上就是vuejs樹形結(jié)構(gòu)數(shù)據(jù)處理的方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于vuejs樹形結(jié)構(gòu)數(shù)據(jù)處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue用Export2Excel導(dǎo)出excel,多級(jí)表頭數(shù)據(jù)方式
這篇文章主要介紹了Vue用Export2Excel導(dǎo)出excel,多級(jí)表頭數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04crypto-js對(duì)稱加密解密的使用方式詳解(vue與java端)
這篇文章主要介紹了如何在Vue前端和Java后端使用crypto-js庫進(jìn)行AES加密和解密,前端通過創(chuàng)建AES.js文件來實(shí)現(xiàn)加密解密功能,并在Vue文件或JavaScript中使用,后端則可以直接使用Java代碼進(jìn)行AES加密和解密操作,需要的朋友可以參考下2025-01-01使用Vue實(shí)現(xiàn)一個(gè)樹組件的示例
這篇文章主要介紹了使用Vue實(shí)現(xiàn)一個(gè)樹組件的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-11-11vue使用pdfjs-dist+fabric實(shí)現(xiàn)pdf電子簽章的思路詳解
最近領(lǐng)導(dǎo)提了一個(gè)新需求:仿照e簽寶,實(shí)現(xiàn)pdf電子簽章,本文給大家介紹vue使用pdfjs-dist+fabric實(shí)現(xiàn)pdf電子簽章的思路,感興趣的朋友一起看看吧2023-12-12vue使用動(dòng)態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式
這篇文章主要介紹了vue使用動(dòng)態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置
這篇文章主要為大家介紹了Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn)
這篇文章主要介紹了vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10