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數組)