思路:先递归b数组,得到 c= [{id,show}]
再递归a数组,遍历c,id相同,修改a
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var ATreeData = [ // A树结构数组
{
id: 1,
text: 1,
show: true,
children: [
{
id: 1.1,
text: 1.1,
show: true
}
],
},
{
id: 2,
text: 2,
show: true
}
]
var BTreeData = [ // B树结构数组
{
id: 1,
text: 1,
show: false,
children: [
{
id: 1.1,
text: 1.1,
show: false
}
],
},
{
id: 2,
text: 2,
show: false
}
]
var CData = [] // 用于保存[{id,show}]
function diguiB(arr) {
arr.forEach((item, index, arr) => {
let obj = {
id: item.id,
show: item.show
}
CData.push(obj)
if (item.children instanceof Array) { // 有children数组
this.diguiB(item.children)
}
});
}
diguiB(BTreeData)
console.log('CData')
console.log(CData)
// 递归数组a
function diguiA(arr) {
arr.forEach((item, index, arr) => {
for (let i = 0; i < CData.length; i++) {
if (item.id == CData[i].id) {
item.show = CData[i].show
}
}
if (item.children instanceof Array) { // 有children数组
this.diguiA(item.children)
}
});
}
diguiA(ATreeData)
console.log('ATreeData')
console.log(ATreeData)
</script>
</body>
</html>