Если слегка изменить условия
- Функция возвращает найденный item (source по нему всегда взять можно)
- Дерево оформлено с корневым узлом
- Функция очищает все остальные active
То получается совсем простая функция
<pre>
<script>
function setActive(str='',tree){
let founditem = null
for (const item of tree.children) {
const res = setActive(str,item)
founditem = founditem || res
}
if (!founditem && tree.link == str) founditem = tree;
tree.active = !! founditem;
return founditem;
}
let tree = {
id:0,
active: false,
link: '',
children: [
{
"id": "9",
"children": [
{
"id": "38",
"children": [],
"active": false,
"link": "page_38"
},
{
"id": "39",
"children": [],
"active": false,
"link": "page_39"
},
{
"id": "40",
"children": [],
"active": false,
"link": "page_40"
},
{
"id": "41",
"children": [],
"active": false,
"link": "page_41"
}
],
"active": false,
"link": "page_9"
},
{
"id": "10",
"children": [
{
"id": "19",
"children": [
{
"id": "13",
"children": [
{
"id": "11",
"children": [],
"active": false,
"link": "page_11"
},
{
"id": "12",
"children": [],
"active": false,
"link": "page_12"
},
{
"id": "14",
"children": [
{
"id": "15",
"children": [],
"active": false,
"link": "page_15"
},
{
"id": "16",
"children": [],
"active": false,
"link": "page_16"
},
{
"id": "17",
"children": [],
"active": false,
"link": "page_17"
},
{
"id": "18",
"children": [],
"active": false,
"link": "page_18"
},
{
"id": "20",
"children": [],
"active": false,
"link": "page_20"
},
{
"id": "21",
"children": [],
"active": false,
"link": "page_21"
},
{
"id": "22",
"children": [],
"active": false,
"link": "page_22"
},
{
"id": "23",
"children": [],
"active": false,
"link": "page_23"
}
],
"active": false,
"link": "page_14"
}
],
"active": false,
"link": "page_13"
},
{
"id": "24",
"children": [],
"active": false,
"link": "page_24"
},
{
"id": "25",
"children": [],
"active": false,
"link": "page_25"
},
{
"id": "28",
"children": [],
"active": false,
"link": "page_28"
}
],
"active": false,
"link": "page_19"
},
{
"id": "26",
"children": [],
"active": false,
"link": "page_26"
},
{
"id": "27",
"children": [],
"active": false,
"link": "page_27"
},
{
"id": "29",
"children": [],
"active": false,
"link": "page_29"
},
{
"id": "30",
"children": [],
"active": false,
"link": "page_30"
},
{
"id": "31",
"children": [],
"active": false,
"link": "page_31"
},
{
"id": "32",
"children": [],
"active": false,
"link": "page_32"
},
{
"id": "33",
"children": [],
"active": false,
"link": "page_33"
},
{
"id": "34",
"children": [],
"active": false,
"link": "page_34"
},
{
"id": "35",
"children": [],
"active": false,
"link": "page_35"
},
{
"id": "36",
"children": [],
"active": false,
"link": "page_36"
},
{
"id": "37",
"children": [],
"active": false,
"link": "page_37"
}
],
"active": false,
"link": "page_10"
},
{
"id": "8",
"children": [],
"active": false,
"link": "page_8"
},
{
"id": "1",
"children": [],
"active": false,
"link": "page_1"
},
{
"id": "3",
"children": [
{
"id": "2",
"children": [],
"active": false,
"link": "page_2"
},
{
"id": "5",
"children": [
{
"id": "4",
"children": [],
"active": false,
"link": "page_4"
}
],
"active": false,
"link": "page_5"
}
],
"active": false,
"link": "page_3"
}
]
}
let fitem = setActive("page_17", tree)
document.write(JSON.stringify(tree, null, ' '))
</script>
</pre>