// 针对 扁平化的 数据 结构
function treeData(source) {
//深克隆
let cloneData = JSON.parse(JSON.stringify(source))
// 父级id === 子级 pid
return cloneData.filter(father => {
let branchArr = cloneData.filter(child => father['id'] == child['pid']);
branchArr.length > 0 ? father['children'] = branchArr : ''
return father['pid'] == 0 // 目前一级 pid:0
})
}
网友评论