简体   繁体   English

在JSON中搜索树结构保留结构

[英]Search in JSON Tree structure preserving Structure

I have a tree structure like below,我有一个像下面这样的树结构,

                     {
                      uid: 1,
                      children: [
                        {
                          uid: 2
                        },
                        {
                          uid: 3,
                          children: [
                            {
                              uid: 4
                            },
                            {
                              uid: 5,
                              children: [
                                {
                                  uid: 6
                                }
                              ]
                            },
                            {
                              uid: 7
                            }
                          ]
                        }
                      ]
                  }

Now, I want to search tree structure by uid and need output as a tree structure(ie along with its parents, but no siblings)现在,我想通过 uid 搜索树结构,需要 output 作为树结构(即与其父母一起,但没有兄弟姐妹)

For example, if I search tree structure for "uid: 4", output result should be something like below,例如,如果我搜索“uid: 4”的树结构,output 结果应该如下所示,

                    {
                      uid: 1,
                      children: [
                        {
                          uid: 3
                          children: [
                            {
                              uid: 4
                            }
                          ]
                        }
                      ]
                  }

I tried recursion but I failed to fetch matching elements along with parents我尝试了递归,但未能与父母一起获取匹配的元素

I came up with an approach for your problem while implementing search in nested json data in UI.在 UI 中的嵌套 json 数据中实现搜索时,我想出了一种解决问题的方法。 Let me know if this helps or you need something else.让我知道这是否有帮助,或者您还需要其他东西。

Here is the working code on stackblitz这是stackblitz 上的工作代码

This algo performs search and recursively updates if the node has to be displayed or not.如果必须显示或不显示节点,此算法将执行搜索并递归更新。 If a node matches the query, along with it, it's children should also be visible.如果一个节点与查询匹配,那么它的子节点也应该是可见的。 When traversing back, algo checks if a any of the node's children is visible, the current node will also be visible.回溯时,算法检查节点的任何子节点是否可见,当前节点也将可见。

As the execution ends, the nodes will have a displayNode property执行结束时,节点将具有displayNode属性

  • true indicates it should be visible true 表示它应该是可见的
  • false indicates it should be hidden false 表示应该隐藏
  // Assuming this object won't contain duplicates
  // 'displayNode' property, either true or false, indicating if the node should be visible
  // show the node if 'displayNode' is true
  // else hide

  // main method
  function search(obj, id) {
    if (obj.children && obj.children.length > 0) {
      for (let i = 0; i < obj.children.length; i++) {
        const child = obj.children[i];
        search(child, id);
      }
    }
    if (obj.uid === id) {
      obj['displayNode'] = true;
      // recursively update the children
      // if a node is true, then all it's children should also be visible
      updateObj(obj);
    } else {
      obj['displayNode'] = false;
      // if any children of this node has 'displayNode' true then this should also be visible
      // to complete the path
      if (obj.children && obj.children.length > 0) {
        if (obj.children.find(y => y.displayNode) !== undefined) {
          obj['displayNode'] = true;
        }
      }
    }
  }

  // update children recursivly
  function updateObj(obj) {
    obj['displayNode'] = true;
    if (obj.children && obj.children.length > 0) {
      for (let i = 0; i < obj.children.length; i++) {
        updateObj(obj.children[i]);
      }
    }
  }

  // call method
  search(obj, 4);
  console.log(obj);

Assuming the original JSON tree has no duplicates, and assuming the JSON tree does not always contain sequential uids, and assuming you only want the direct ancestral line, it might be best to use a depth first search ( https://www.tutorialspoint.com/Depth-first-search-traversal-in-Javascript ) on the original JSON tree. 假设原始JSON树没有重复项,并且假设JSON树并不总是包含顺序的uid,并且假设您只想要直接祖先线,则最好使用深度优先搜索( https://www.tutorialspoint。 com / Depth-first-search-traversal-in-Javascript )。 You'll also want to keep track of the current direct ancestral line in case the next value matches the one you are searching for. 您还需要跟踪当前的祖先直线,以防下一个值与您要搜索的那个相匹配。 Then you can return the full direct line. 然后,您可以返回完整的直线。

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

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