用抛出异常跳出循环
try {
const recursion = array => {
array.some(tree => {
if (this.nodeTypeList.includes(tree.nodeType)) {
selectable = tree
throw new Error()
} else if (tree.children && tree.children.length) {
recursion(tree.children)
}
})
}
recursion(this.treeDataPublish)
} catch (error) {
console.log(error)
}
用return跳出循环:
1、some:return true适用于找到第一条就跳出的
const recursion = array => {
return array.some(tree => {
if (this.nodeTypeList.includes(tree.nodeType)) {
selectable = tree
return true
} else if (tree.children && tree.children.length) {
return recursion(tree.children)
}
})
}
recursion(this.treeDataPublish)
这里的需要注意的是 递归里面也要return recursion 否则达不到找到一条就跳出循环的效果