Есть такой массив:
const dataArr = [
{
"id": 28,
"title": "Бесплатные материалы",
"children": [
{
"id": 29,
"title": "Lorem",
"children": [
{
"id": 32,
"title": "Lorem",
"children": []
},
{
"id": 30,
"title": "Lorem",
"children": []
}
],
},
{
"id": 33,
"title": "Lorem",
"children": [
{
"id": 35,
"title": "Lorem",
"children": []
},
{
"id": 34,
"title": "Lorem",
"children": []
}
],
}
],
}
]
Ещё есть переменная, содержащая id объекта:
let objID = 29.
И есть рекурсивная функция, которая обрабатывает этот массив:
funtion test1(arr) {
let res = [];
arr.forEach(item => {
if (item.id === objID) {
res.push(item)
} else {
test1(item.children)
}
});
console.log('funtion test1', res)
return res
}
console.log('result', test1(dataArr))
Ожидаемый результат:
result = [ {
"id": 29,
"title": "Lorem",
"children": [
{
"id": 32,
"title": "Lorem",
"children": []
},
{
"id": 30,
"title": "Lorem",
"children": []
}
],
},
]
Но результат такой, и непонятно почему:

Как сделать так. чтобы он пустые ответы не отдавал?