共计 785 个字符,预计需要花费 2 分钟才能阅读完成。
比拟递归
const compare = (data1, data2) => {
data1.forEach(item1 => {
data2.forEach(item2 => {if (item1.id === item2.id && item1.children && item2.children) {if (item1.children.length === item2.children) {this.checkedKeys.push(item1.id)
} else {
item2.children.forEach(item3 => {this.checkedKeys.push(item3.id)
})
}
} else {compare(item1, item2)
}
})
})
}
compare(this.treeData, res.result)
寻找父级递归
let parentObj = this.treeData[0]
console.log(parentObj)
const findParentId = (treeData, key) => {
let flag = false
treeData.forEach(item => {if (item.id === key) {flag = true} else if (item.children && item.children.length > 0) {
parentObj = item
findParentId(item.children, key)
}
})
if (flag && !resultKeys.includes(parentObj.id)) {resultKeys.push(parentObj.id)
}
}
this.checkedKeys.forEach(key => {findParentId(this.treeData, key)
if (!resultKeys.includes(key)) {resultKeys.push(key)
}
})
正文完
发表至: javascript
2022-05-25