代码如下
export function TreeDataFindItem({
origin = [],
childrenName = 'children',
fieldName = 'id',
value = ''
}) {
let item = null
function loop(arr) {
console.log(arr)
for (let i = 0; i < arr.length; i++) {
const x = arr[i]
debugger
if (x[fieldName] === value) {
item = x
break
}
if (Array.isArray(x[childrenName]) && x[childrenName].length) {
loop(x[childrenName])
}
}
}
loop(origin)
if (item) return item
return null
}
使用方法
const item = TreeDataFindItem({
origin: [{ id: 'aa', children: [{ id: 'bb' }] }],
value: 'bb'
})
console.log(item)