🐶
function find(array, param, paramKey = "id", childrenKey = "children") {
let res
for (let index = 0; index < array.length; index++) {
function fn() {
if (array[index][paramKey] === param) return array[index]
if (array[index][childrenKey] && array[index][childrenKey].length)
return find(array[index][childrenKey], param)
}
if (fn()) {
res = fn()
break
}
}
return res
}
const arr = [{
"id": 1,
"children": [{
"id": 2,
},
{
"id": 3,
"children": [{
"id": 4,
}]
}
]
},
{
"id": 5,
"children": [{
"id": 6,
}]
}
]
console.log(find(arr, 1))
console.log(find(arr, 999))
console.log(find(arr, 6))