const tree = [{"title":"根目录","expand":true,"id":"12","children":[{"title":"resource-center","id":"8a058e80eda611ea81408f6608aed1cc","expand":true,"children":[{"title":"332","id":"97224830edc611ea85fd83480dc4bc9c","expand":false,"children":[],"loading":false,"nodeKey":2}],"loading":false,"nodeKey":1},{"title":"344","id":"95261270edc311ea92d43b07cb225f6f","expand":false,"children":[],"loading":false,"nodeKey":3},{"title":"ererere","id":"33abf3a0ee5f11eaa669016650fd27b2","expand":false,"children":[],"loading":false,"nodeKey":4},{"title":"ererer","id":"3e9d2ec0ee6111eab2ea6dbf0dfbf381","expand":false,"children":[],"loading":false,"nodeKey":5}],"loading":false,"nodeKey":0}]
function makePath(tree, target) {
var done = false, path = [];
function traverse(tree, target) {
for (let i = 0; i < tree.length; i++) {
const node = tree[i];
if (!done) {
if (i > 0) {
// path.pop();
}
path.push(node.id);
if (node.id === target) {
done = true;
return;
} else {
let children = node.children;
if (children) {
traverse(children, target);
}
}
}
}
if (!done) {
path.pop();
}
return;
};
traverse(tree, target);
return path;
};
console.log(makePath(tree, '97224830edc611ea85fd83480dc4bc9c'), 1)
console.log(makePath(tree, '95261270edc311ea92d43b07cb225f6f'), 1)
console