繁体   English   中英

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

[英]Search in JSON Tree structure preserving Structure

我有一个像下面这样的树结构,

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

现在,我想通过 uid 搜索树结构,需要 output 作为树结构(即与其父母一起,但没有兄弟姐妹)

例如,如果我搜索“uid: 4”的树结构,output 结果应该如下所示,

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

我尝试了递归,但未能与父母一起获取匹配的元素

在 UI 中的嵌套 json 数据中实现搜索时,我想出了一种解决问题的方法。 让我知道这是否有帮助,或者您还需要其他东西。

这是stackblitz 上的工作代码

如果必须显示或不显示节点,此算法将执行搜索并递归更新。 如果一个节点与查询匹配,那么它的子节点也应该是可见的。 回溯时,算法检查节点的任何子节点是否可见,当前节点也将可见。

执行结束时,节点将具有displayNode属性

  • true 表示它应该是可见的
  • 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);

假设原始JSON树没有重复项,并且假设JSON树并不总是包含顺序的uid,并且假设您只想要直接祖先线,则最好使用深度优先搜索( https://www.tutorialspoint。 com / Depth-first-search-traversal-in-Javascript )。 您还需要跟踪当前的祖先直线,以防下一个值与您要搜索的那个相匹配。 然后,您可以返回完整的直线。

暂无
暂无

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

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