[
  {
    "secCode": 2,
    "secName": "GENERAL NURSING CARE SECTION",
    "prevSec": 0,

  },
  {
    "secCode": 1,
    "secName": "CRITICAL CARE NURSING SECTION",
    "prevSec": 0,
    "children": [
      {
        "secCode": 3,
        "secName": "OPERATION THEATRE",
        "prevSec": 1,
        "children": [
          {
            "secCode": 5,
            "secName": "MAIN OPERATION THEATRE",
            "prevSec": 3,
            "estCode": 152,

          },
          {
            "secCode": 6,
            "secName": "DAY CARE DT SERVICE",
            "prevSec": 3,

          }
        ]
      },
      {
        "secCode": 4,
        "secName": "CRITICAL CARE SERVICES",
        "prevSec": 1,
        "children": [
          {
            "secCode": 675,
            "secName": "Test",
            "prevSec": 4,
            "children": [
              {
                "secCode": 676,
                "secName": "Test1",
                "prevSec": 675,

              },
              {
                "secCode": 677,
                "secName": "Test 2",
                "prevSec": 675,

              },
              {
                "secCode": 678,
                "secName": "Test 3",
                "prevSec": 675,

              },
              {
                "secCode": 679,
                "secName": "Test 4",
                "prevSec": 675,

              }
            ]
          },
          {
            "secCode": 7,
            "secName": "ACUTE CARE",
            "prevSec": 4,

          }
        ]
      }
    ]
  }
]


这是树json树结构。在这里,每个节点都使用prevSec值递归向后连接到其父节点。
对于secCode 7,其父树将为[7,4,1]-通过secCode = prevSec连接。
对于secCode 679,其父母将为[679、675、4、1]
对于secCode 2,其父母将为[2]

我尝试了这种解决方案,这是部分的

 getParent(arr, childSecCode) {

        if (childSecCode == 0) {
          return;
        } else {
          let val = arr.find(item => {
           childSecCode === item.secCode;
          });
          if (val) {
            //arr.find(item => childSecCode == item.secCode).showChildren = true;
           /*  if(check ==0)
            arr.find(item => childSecCode == item.secCode).className = "selected"; */
            this.getParent(arr, val.prevSec);
          }
        }
      }


这将不起作用,因为find不会检查孩子。

我怎样才能达到预期的效果。

最佳答案

如果要在需要时避免对整个树进行搜索,最好先对树进行预处理,以便在给定secCode的情况下,可以在恒定时间内直接找到任何节点(对象)。

您可以为Map和节点之间的连接创建一个secCode



// Creates a map which is keyed by secCode, and for a secCode provides
//   the corresponding node from the tree
function createMap(tree) {
    let map = new Map;

    function recur(node) {
        map.set(node.secCode, node);
        if (node.children) node.children.forEach(recur);
    }

    tree.forEach(recur);
    return map;
}

// Uses the map to walk up the tree
function getParents(secCode, map) {
    let parents = [];
    while (secCode) {
        parents.push(secCode);
        let node = map.get(secCode);
        secCode = node.prevSec;
    }
    return parents;
}

// The tree from the question:
let tree = [{"secCode": 2,"secName": "GENERAL NURSING CARE SECTION","prevSec": 0,},{"secCode": 1,"secName": "CRITICAL CARE NURSING SECTION","prevSec": 0,"children": [{"secCode": 3,"secName": "OPERATION THEATRE","prevSec": 1,"children": [{"secCode": 5,"secName": "MAIN OPERATION THEATRE","prevSec": 3,"estCode": 152,},{"secCode": 6,"secName": "DAY CARE DT SERVICE","prevSec": 3,}]},{"secCode": 4,"secName": "CRITICAL CARE SERVICES","prevSec": 1,"children": [{"secCode": 675,"secName": "Test","prevSec": 4,"children": [{"secCode": 676,"secName": "Test1","prevSec": 675,},{"secCode": 677,"secName": "Test 2","prevSec": 675,},{"secCode": 678,"secName": "Test 3","prevSec": 675,},{"secCode": 679,"secName": "Test 4","prevSec": 675,}]},{"secCode": 7,"secName": "ACUTE CARE","prevSec": 4,}]}]}];
// Preprocessing
let map = createMap(tree);
// Example calls:
console.log(getParents(7, map));
console.log(getParents(679, map));





请注意,示例中的树没有3作为其子代暗示的3-4是1的子代。

如果有一个类,则当然可以在其构造函数中创建该映射并将其分配给属性:



class Tree {
    constructor(data) {
        this.map = new Map;

        const recur = (node) => {
            this.map.set(node.secCode, node);
            if (node.children) node.children.forEach(recur);
        }

        data.forEach(recur);
    }
    getParents(secCode) {
        let parents = [];
        while (secCode) {
            parents.push(secCode);
            secCode = this.map.get(secCode).prevSec;
        }
        return parents;
    }
}

// The tree from the question:
let data = [{"secCode": 2,"secName": "GENERAL NURSING CARE SECTION","prevSec": 0,},{"secCode": 1,"secName": "CRITICAL CARE NURSING SECTION","prevSec": 0,"children": [{"secCode": 3,"secName": "OPERATION THEATRE","prevSec": 1,"children": [{"secCode": 5,"secName": "MAIN OPERATION THEATRE","prevSec": 3,"estCode": 152,},{"secCode": 6,"secName": "DAY CARE DT SERVICE","prevSec": 3,}]},{"secCode": 4,"secName": "CRITICAL CARE SERVICES","prevSec": 1,"children": [{"secCode": 675,"secName": "Test","prevSec": 4,"children": [{"secCode": 676,"secName": "Test1","prevSec": 675,},{"secCode": 677,"secName": "Test 2","prevSec": 675,},{"secCode": 678,"secName": "Test 3","prevSec": 675,},{"secCode": 679,"secName": "Test 4","prevSec": 675,}]},{"secCode": 7,"secName": "ACUTE CARE","prevSec": 4,}]}]}];
// Preprocessing
let tree = new Tree(data);
// Example calls:
console.log(tree.getParents(7));
console.log(tree.getParents(679));

10-06 12:02