js實現el tree樹形資料轉為扁平資料

2022-09-07 19:33:14 字數 811 閱讀 7977

element*****====截圖

解決方法:

利用遞迴的方法迴圈樹形資料,當遇到有children的物件再次強調有遞迴函式迴圈children陣列,每次迴圈的資料放入乙個提前生命好的陣列裡,等所有的遞迴函式執行完畢,這個資料就是想要的扁平資料陣列,如上圖。

let res =         // 用於儲存遞迴結果(扁平資料)

// 遞迴函式

const fn = (source)=>)

}

示例1

let res =         // 用於儲存遞迴結果(扁平資料)

// 遞迴函式

const fn = (source)=>)}

// 樹形資料

const arr = [,,

] },

] }

] }

] }

] }]

fn(arr) // 執行遞迴函式

console.log(res) // 檢視結果

結果

Vue樹形資料處理 js

當前需求 層級列表中選中某個元素,則獲取最底層子集id,並用逗號連線 1,2,3,4,5 let data 遍歷方法可參考 寫的很詳細 具體操作 首先找到選擇元素的id在樹形資料中的位置,並獲取他 findsameid tree,id if id tree i id isget deepsearch...

樹形資料轉換

測試資料 create table project id int,name nvarchar 20 parent id int insert project select 1,所有專案 null union all select 2,專案1 1 union all select 3,專案2 1 cr...

樹形資料轉換

測試資料 create table project id int,name nvarchar 20 parent id int insert project select 1,所有專案 null union all select 2,專案1 1 union all select 3,專案2 1 cr...