// @path 供递归使用 function findPathByLeafId(leafId, nodes, path) { if (path === undefined) { path = []; for ( var i = 0 ; i < nodes.length; i++ ) { var tmpPath = path.concat(); tmpPath.push(nodes[i].id); if (leafId == nodes[i].id) { return tmpPath; if (nodes[i].children) { var findResult = findPathByLeafId(leafId, nodes[i].children, tmpPath); if (findResult) { return findResult;
console.log(findPathByLeafId(4, nodes))

改造
获取整个obj数据
function findPathByLeafId(leafId, nodes, path){
    if(path === undefined) {
        path = {};
      for(var i = 0; i < nodes.length; i++) {
          var tmpPath = path;
        //   tmpPath.push(nodes[i].id);
          if(leafId == nodes[i].id) {
            tmpPath=nodes[i];
            return tmpPath;
          if(nodes[i].children) {
            var findResult = findPathByLeafId(leafId, nodes[i].children,  tmpPath);
            if(findResult) {
              return findResult;

原文:https://blog.csdn.net/m0_37727560/article/details/91607575