js實(shí)現(xiàn)樹形數(shù)據(jù)轉(zhuǎn)成扁平數(shù)據(jù)的方法示例
利用遞歸的方法循環(huán)樹形數(shù)組,當(dāng)遇到有children的對象再次調(diào)用遞歸函數(shù)循環(huán)children數(shù)組,每次循環(huán)的數(shù)據(jù)放入一個(gè)提前聲明好的數(shù)組里,等所有遞歸函數(shù)執(zhí)行完,這個(gè)數(shù)組即是想要得到的扁平數(shù)據(jù)數(shù)組。
let res = [] const fn = (source)=>{ source.forEach(el=>{ res.push(el) el.children && el.children.length>0 ? fn(el.children) : "" }) }
示例1
let res = [] // 用于存儲遞歸結(jié)果(扁平數(shù)據(jù)) // 遞歸函數(shù) const fn = (source)=>{ source.forEach(el=>{ res.push(el) el.children && el.children.length>0 ? fn(el.children) : "" // 子級遞歸 }) } // 樹形數(shù)據(jù) const arr = [ { id: "1", rank: 1 }, { id: "2", rank: 1, children:[ { id: "2.1", rank: 2 }, { id: "2.2", rank: 2 } ] }, { id: "3", rank:1, children:[ { id: "3.1", rank:2, children: [ { id:'3.1.1', rank:3, children:[ { id: "3.1.1.1", rank: 4, children:[ { id: "3.1.1.1.1", rank: 5 } ] } ] } ] } ] } ] fn(arr) // 執(zhí)行遞歸函數(shù) console.log(res) // 查看結(jié)果
結(jié)果:
扁平數(shù)據(jù)轉(zhuǎn)成樹形數(shù)據(jù),請參考這篇文章:js實(shí)現(xiàn)無限層級樹形數(shù)據(jù)結(jié)構(gòu)(創(chuàng)新算法)
js將扁平結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換為樹形結(jié)構(gòu)
遞歸實(shí)現(xiàn)
function transformTree (list) { const tree = [] for (let i = 0, len = list.length; i < len; i++) { if (!list[i].pid) { const item = queryChildren(list[i], list) tree.push(item) } } return tree } function queryChildren (parent, list) { const children = [] for (let i = 0, len = list.length; i < len; i++) { if (list[i].pid === parent.id) { const item = queryChildren(list[i], list) children.push(item) } } if (children.length) { parent.children = children } return parent }
盡管后續(xù)對上面的算法進(jìn)行了很多優(yōu)化,但是仍未離開遞歸,遞歸可能遇到的問題還是會有可能遇到
循環(huán)實(shí)現(xiàn)
隨著進(jìn)化,循環(huán)代替遞歸是必然的結(jié)果~
兩次循環(huán)
開始使用循環(huán)實(shí)現(xiàn)時(shí),使用了兩次循環(huán)完成轉(zhuǎn)換,先進(jìn)行一次循環(huán)將數(shù)據(jù)轉(zhuǎn)換成 map 結(jié)構(gòu),使其能通過 id 快速查詢
function transformTree (list) { const tree = [] const record = {} const length = list.length for (let i = 0; i < length; i++) { const item = list[i] item.children = [] // 重置 children record[item.id] = item } for (let i = 0; i < length; i++) { const item = list[i] if (item.pid) { if (record[item.pid]) { record[item.pid].children.push(item) } } else { tree.push(item) } } return tree }
上面的算法相較于遞歸的實(shí)現(xiàn),不存在棧溢出的問題,而且是線性復(fù)雜度,效率已經(jīng)提高了許多
一次循環(huán)
再進(jìn)行一定的優(yōu)化,最后變成一次循環(huán)完成樹形構(gòu)建
function transformTree (list) { const tree = [] const record = {} for (let i = 0, len = list.length; i < len; i++) { const item = list[i] const id = item.id if (record[id]) { item.children = record[id] } else { item.children = record[id] = [] } if (item.pid) { if (!record[item.pid]) { record[item.pid] = [] } record[item.pid].push(item) } else { tree.push(item) } } }
使用對象變量的特性,使用 map 結(jié)構(gòu)直接指向 children 數(shù)組,在循環(huán)中初始化的同時(shí)還能快速查找插入相應(yīng)的 children 里,使其在一次循環(huán)內(nèi)完成構(gòu)建,最后附上完整版~
function transformTree (list, options = {}) { const { keyField = 'id', childField = 'children', parentField = 'parent' } = options const tree = [] const record = {} for (let i = 0, len = list.length; i < len; i++) { const item = list[i] const id = item[keyField] if (!id) { continue } if (record[id]) { item[childField] = record[id] } else { item[childField] = record[id] = [] } if (item[parentField]) { const parentId = item[parentField] if (!record[parentId]) { record[parentId] = [] } record[parentId].push(item) } else { tree.push(item) } } return tree }
到此這篇關(guān)于js實(shí)現(xiàn)樹形數(shù)據(jù)轉(zhuǎn)成扁平數(shù)據(jù)的方法示例的文章就介紹到這了,更多相關(guān)js 樹形數(shù)據(jù)轉(zhuǎn)成扁平數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript 對象屬性property與元素屬性attribute的瀏覽器支持
對象屬性property與元素屬性attribute的瀏覽器支持情況,大家可以參考下。2010-10-10BootStrap Fileinput插件和Bootstrap table表格插件相結(jié)合實(shí)現(xiàn)文件上傳、預(yù)覽、提交的導(dǎo)入E
這篇文章主要介紹了BootStrap Fileinput插件和Bootstrap table表格插件相結(jié)合實(shí)現(xiàn)文件上傳、預(yù)覽、提交的導(dǎo)入Excel數(shù)據(jù)操作步驟,需要的朋友可以參考下2017-08-08js中如何把字符串轉(zhuǎn)化為對象、數(shù)組示例代碼
在本文為大家介紹下把字符串轉(zhuǎn)化為對象:把文本轉(zhuǎn)化為對象、把文本轉(zhuǎn)化為數(shù)組,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07js 基礎(chǔ)篇必看(點(diǎn)擊事件輪播圖的簡單實(shí)現(xiàn))
下面小編就為大家?guī)硪黄猨s 基礎(chǔ)篇必看(點(diǎn)擊事件輪播圖的簡單實(shí)現(xiàn))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08javascript實(shí)現(xiàn)數(shù)獨(dú)解法
數(shù)獨(dú)(すうどく,Sūdoku)是一種運(yùn)用紙、筆進(jìn)行演算的邏輯游戲。玩家需要根據(jù)9×9盤面上的已知數(shù)字,推理出所有剩余空格的數(shù)字,并滿足每一行、每一列、每一個(gè)粗線宮內(nèi)的數(shù)字均含1-9,不重復(fù)。2015-03-03初學(xué)js插入節(jié)點(diǎn)appendChild insertBefore使用方法
由于可見insertBefore()方法的特性是在已有的子節(jié)點(diǎn)前面插入新的節(jié)點(diǎn)但是兩種情況結(jié)合起來發(fā)現(xiàn)insertBefore()方法插入節(jié)點(diǎn),是可以在子節(jié)點(diǎn)列表的任意位置。2011-07-07js實(shí)現(xiàn)3D粒子酷炫動態(tài)旋轉(zhuǎn)特效
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)3D粒子酷炫動態(tài)旋轉(zhuǎn)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09基于JS實(shí)現(xiàn)數(shù)字動態(tài)變化顯示效果附源碼
我們經(jīng)??吹揭壕щ娮颖順邮剑瑪?shù)字動態(tài)顯示,動態(tài)變化的在指定元素內(nèi)顯示數(shù)字。怎么實(shí)現(xiàn)效果呢?下面小編給大家?guī)砹嘶贘S實(shí)現(xiàn)數(shù)字動態(tài)變化顯示效果 ,感興趣的朋友一起看看吧2019-07-07