1、根据树tree的key获取对应的树的节点item
export const findTreeItem = (tree = [], keys= '') => {
for (const item of tree) {
if (item.dataIndex == keys) {
return item
} else {
if (item.children && item.children.length) {
const treeItem = this.findTreeItem(item.children, keys)
if (treeItem) {
return treeItem
}
}
}
}
}
2、树数据匹配, B的children对象值覆盖A的children对象值
export const handleTreeMatch = (treeA = [], treeB = []) => {
const updateList = treeA.map(item => {
const updateChildren = item.children.map(child => {
let treeBItem = this.findTreeItem(treeB, child.key)
return { ...child, ...treeBItem}
})
return { ...item, children: updateChildren }
})
return updateList
},
3、使用
this.treeA = this.handleTreeMatch (treeA, treeB)