dpts,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</head>
<body>
<script src="https://nervgh.github.io/js/recursive-iterator.min.js"></script>
<pre>
<script>
var link = 'page_24',
tree = [
{
"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"
}
];
const setParentsActiveValue = (link, source, value) => {
for (let {node, path} of new RecursiveIterator(source)) {
if (node == link) {
console.log(path.join('.'), node);
let obj = source;
for (let key of path) {
obj = obj[key];
if (obj.hasOwnProperty('active')) {
console.log("id", obj.id);
obj.active = value
}
}
break;
}
}
return source
}
document.write(JSON.stringify(setParentsActiveValue(link,tree, true), "", 2))
</script>
</pre>
</body>
</html>