简体   繁体   English

从具有父子引用的数组创建树结构

[英]Create a tree structure from an array with parent-child references

I am trying to alter the json in snippet to a tree structure just like in https://www.primefaces.org/primeng/#/treetable (below is the sample i expect too).我正在尝试将片段中的 json 更改为树结构,就像在https://www.primefaces.org/primeng/#/treetable中一样(下面是示例)。 I understand it involves recursion but I ain't sure how to deeply link each.我知道它涉及递归,但我不确定如何深入链接每个。

The output i expect is something like below.我期望的 output 如下所示。 The json whose parent is true becomes the root.其父为 true 的 json 成为根。 If the root has values, the json corresponding to id of the value is pushed to children array with a json object "data".如果根有值,则与该值的 id 对应的 json 被推送到子数组,其中 json object “数据”。 Again if that json has values, the json correspond to the id of value is pushed to children array with a json object "data and so on.同样,如果 json 有值,则对应于值的 id 的 json 将被推送到子数组,其中包含 json ZA8CFDE633111BD59EB2AC96Fdata 和 so on

The code i have written is just a initial phase.我写的代码只是一个初始阶段。 Need help on how nesting can be done through iteration.需要有关如何通过迭代完成嵌套的帮助。

[
  {
    "data": {
      "parent": true,
      "id": "C001",
      "type": "Folder",
      "values": [
        {
          "id": "P001",
          "type": "File"
        }
      ]
    },
    "children": [
      {
        "data": {
          "parent": false,
          "id": "P001",
          "type": "File",
          "values": [
            {
              "id": "P002",
              "type": "Image"
            }
          ]
        },
        "children": [
          {
            "data": {
              "parent": false,
              "id": "P002",
              "type": "Image",
              "values": [

              ]
            }
          }
        ]
      }
    ]
  },
  {
    "data": {
      "parent": true,
      "id": "S000",
      "type": "Something",
      "values": [

      ]
    }
  }
]

 var junkdata=[ { "parent": false, "id": "P001", "type":"File", "values": [ { "id": "P002", "type": "Image" } ] }, { "parent": true, "id": "C001", "type": "Folder", "values": [ { "id": "P001", "type": "File" }] }, { "parent": false, "id": "P002", "type": "Image", "values":[] }, { "parent": true, "id": "S000", "type": "Something", "values":[] }]; var parentDatas=junkdata.filter((x)=>x.parent==true); if(parentDatas.length>0){ var finalResponse=parentDatas.map((parentData)=>{ var resultJson={}; resultJson.data=parentData; if(parentData.values.length>0){ resultJson.children=[]; for(var i of parentData.values){ var child=junkdata.find((x)=>x.id==i.id); if(child){ var jsonObj={}; jsonObj.data=child; resultJson.children.push(jsonObj); } } } return resultJson; }) } console.log(JSON.stringify(finalResponse));

Basically, we can start with this to process the root nodes:基本上,我们可以从这个开始处理根节点:

let tree = yourData.filter(x => x.parent).map(process);

where process is the recursive function that processes a given node:其中process是处理给定节点的递归 function:

let process = node => ({
    id: node.id,
    type: node.type,
    children: node.values.map(x => process(
        yourData.find(y => y.id === x.id)))
});

For each id in node.values , it locates a node with that id and recursively calls process on it.对于node.values中的每个id ,它会找到具有该id的节点并递归调用它的process Once all child nodes are dealt with, process collects them into an array and returns the newly formatted object.处理完所有子节点后, process将它们收集到一个数组中并返回新格式化的 object。

This is the general recursion pattern for working with graph-alike structures, where you have "nodes" somehow connected to other "nodes":这是使用类似图形的结构的一般递归模式,其中“节点”以某种方式连接到其他“节点”:

function F (N: node) {

    for each node M which is connected to N {
        F (M) <--- recursion
    }

    result = do something with N

    return result
}

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

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