Cascading selectors usually only store the last bit, but when echoing, the node value of the entire directory is needed. At this time, it is necessary to traverse to obtain the value of the parent node
var path = [];
function getPath(tree, id, path) {
for (var i = 0; i < tree.length; i++) {
const node = tree[i];
if (node.value == id) {
path.push(node.value)
return path
} else {
if (node.children && node.children.length) {
if (getPath(node.children, id, path)) {
path.push(node.value)
return path
}
}
}
}
}