繁体   English   中英

如何获取树的每个节点的父母和所有孩子的数组?

[英]How can I get an array of the parents and all of the children of each node of a tree?

我有一种数据,图中就是它的一个例子

[
  {
    id: "1",
    parentId: "0"
  },
  {
    id: "2",
    parentId: "0",
  },
  {
    id: "3",
    parentId: "1"
  },
  {
    id: "4",
    parentId: "2"
  }
]

在此处输入图像描述

如何获取每个节点的父节点和所有子节点的数组。

例如:

Input: 1
Output: [0,1,3]
Input: 3
Output: [0,1,3]
Input: 0
Output: [0,1,3],[0,2,4]

所以这里有一个非常粗略的代码来实现你想要实现的。 当然可以改进,但我认为在功能上这应该可以工作->

start: function() {
 var arr = [
   {
     id: "1",
     parentId: "0",
   },
   {
     id: "2",
     parentId: "0",
   },
   {
     id: "3",
     parentId: "1",
    },
    {
     id: "4",
     parentId: "2",
    },
  ];
 var out = [];
 var input = "2";
 var finalOut = [];

 this.getParentArray(arr, input, out);
 console.log("out", out);
 this.getChildArray(arr, input, out, finalOut);
 console.log("finalOut", finalOut);
}



/** 
   * Get parents of the input
   */
  getParentArray: function(flatArray = [], input, output = []) {
    // Find the node
    let node = flatArray.find((f) => f.id == input);
    if (node) {
      output.unshift(node.id);
      // Find parent of the node
      let parentNode = flatArray.find((f) => f.id == node.parentId);
      if (parentNode) {
        // If parent node has further parents, do recursion to get all parents
        if (flatArray.findIndex((f) => f.id == parentNode.parentId) != -1) {
          this.getParentArray(flatArray, parentNode.id, output);
        } else {
          // Add parent and it's parent id to the output
          output.unshift(parentNode.id);
          output.unshift(parentNode.parentId);
        }
      } else {
        // If no parent, add the node parent id on top
        output.unshift(node.parentId);
      }
    } else if (flatArray.findIndex((f) => f.parentId == input) != -1) {
      // If no node found and the input exists as parent id to some element, then add this input in the last
      output.push(input);
    }
  }
  /** 
   * Get children of the array
   */
  getChildArray: function(flatArray = [], input, output = [], finalOutput = []) {
    // Get all children of the input
    let children = flatArray.filter((f) => f.parentId == input);
    // Create a middle point to hold the data
    let midOutput = JSON.parse(JSON.stringify(output));
    if (!children || children.length == 0) {
      // If no children, just push the output to final output
      finalOutput.push(output);
      return;
    }
    children.forEach((child, index) => {
      midOutput = JSON.parse(JSON.stringify(output));
      if (child) {
        midOutput.push(child.id);
        // Recursion if child has more children
        if (flatArray.findIndex((f) => f.parentId == child.id) != -1) {
          this.getChildArray(flatArray, child.id, midOutput, finalOutput);
        } else {
          finalOutput.push(JSON.parse(JSON.stringify(midOutput)));
        }
      }
    });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM