1、先看數據結構
2、實現代碼
//獲取樹形結構的所有節點
lookForAllId(data = [], arr = []) { for (let item of data) { arr.push(item); if (item.children && item.children.length) this.lookForAllId(item.children, arr); } return arr; },
3、調用
let arrAll = this.lookForAllId(this.treedata); //this.treedata為樹形結構的數據 arrAll.forEach((item2) => { console.log(item2) //所有的節點 //判斷是否還有子級 if (item2.children == null) { //沒有子級 }else{ //有子級 });
看打印結果
完結!