treeData(arr) { let cloneData = JSON.parse(JSON.stringify(arr)) //先將原來的陣列深拷貝一份,防止影響到原來的資料 return cloneData.filter(father => { let branchArr = cloneData.filter(child => father.id == child.parentId); branchArr.length > 0 ? father.children = branchArr : '' return father.parentId == null //回傳第一層的資料,如果最上一級的父級id不是null,請自行修改 }) } 呼叫 this.treeData(json陣列)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/53646.html
標籤:JavaScript
上一篇:js獲取cookie值
