已知tree ,给树的每个对象加一个字段 (isDisable: true)或者对象key名字修改,最后得到一个tree
traverseToDisabled(list) {
const dealFun = function (list) {
return _.map(list, (item) => {
if (_.get(item, 'children.length', 0)) {
return {
...item,
isDisable: true,
children: dealFun(item.children)
}
} else {
return { ...item, isDisable: true }
}
})
}
return dealFun(list)
},
已知tree, 只要 checked=true的对象,最后还是一个tree
const dealFun = function (list) {
const sunList = _.filter(list, (item) => item.checked)
return _.map(sunList, (item) => {
if (_.get(item, 'children.length', 0)) {
return {
...item,
children: dealFun(item.children)
}
} else {
return item
}
})
}
const list = _.get(this.currItem, 'resourceList', [])
console.log(dealFun(list), ' dealFun(list)')
已知tree ,对象为checked=true时,把tree对象push,最后得到一个数组
const checkedNodes = []
const traverse = function (node) {
node.forEach((child) => {
if (child.checked && _.get(child, 'children.length', 0) == 0) {
checkedNodes.push(child)
}
traverse(child.children)
})
}
const list = _.get(this.data, 'resourceList', [])
traverse(list)
已知tree ,把tree最后节点对象push(且对象过滤掉index这个字段),最后得到一个数组
getArgs(list) {
let subList = []
const dealFun = function (list) {
for (let index = 0
if (_.get(list[index], 'children.length', 0)) {
dealFun(list[index].children)
} else {
subList.push(_.omit(list[index], ['index']))
}
}
}
dealFun(list)
return subList
}
已知tree和一个数组(id集合),tree中得对象的id和数组中id一样时,把tree对象push,最后得到一个数组
traverse(list, subList) {
let checkedNodes = []
const traverse = function (node = []) {
for (let i = 0; i < node.length; i++) {
if (_.get(node[i], 'children.length', 0) == 0 && _.includes(subList, node[i].id)) {
checkedNodes.push(node[i].id)
}
traverse(node[i].children)
}
}
traverse(list)
return checkedNodes
},
已知tree和一个数组,tree中得对象的id和数组对象id一样时,把数组对象赋值给tree对象,最后得到一个tree
traverseToOption(list, subList) {
const dealFun = function (list) {
return _.map(list, (item, index) => {
const editItem = _.find(subList, (ele) => ele.typeId === item.id)
const newItem = {
index,
typeId: _.get(editItem, 'typeId', item.id) || item.id,
reason: _.get(editItem, 'reason', ''),
situation: _.get(editItem, 'situation', 3) || 3,
typeName: _.get(editItem, 'typeName', item.name) || item.name
}
if (_.get(item, 'children.length', 0)) {
return {
...newItem,
children: dealFun(item.children)
}yi
} else {
return newItem
}
})
}
return dealFun(list)
},

已知tree,在根节点和内容节点上添加字段计算叶子节点数
function calc(node) {
if (!node) return 0
const children = node.children
const childCount = node.children?.length ? 0 : 1
if (!children) return childCount
const value = childCount + children.reduce((v, child) => v + calc(child), 0)
node.childCount = value
return value
}
list.forEach((i) => calc(i))
已知tree,获取所有叶子节点的对象.组成一个数组
let leafList = []
function getTreeLeaf(treeData) {
if (Array.isArray(treeData)) {
treeData.forEach((item) => {
if (item.children && item.children.length > 0) {
getTreeLeaf(item.children, leafList)
} else {
leafList.push(item)
}
})
} else {
if (treeData.children && treeData.children.length > 0) {
getTreeLeaf(treeData.children, leafList)
} else {
leafList.push(treeData)
}
}
}
getTreeLeaf(list)
已知tree,获取某个id的对象
const findNodeById = function (nodes, id) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
if (node.id === id) {
return node
} else if (node.children) {
const result = findNodeById(node.children, id)
if (result) {
return result
}
}
}
return null
}
findNodeById(areaOptions, areaId)
```