javascript中树状结构中的搜索过滤器?

javascript中树状结构中的搜索过滤器?,javascript,Javascript,我有一个树结构的数据应该被过滤,结果应该保留树结构 var tree = [ { text: "Parent 1", nodes: [ { text: "Child 1", type: "Child", nodes: [ { text: "Grandchild 1" type: "Grandchild" },

我有一个树结构的数据应该被过滤,结果应该保留树结构

var tree = [
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        type: "Child",
        nodes: [
          {
            text: "Grandchild 1"
            type: "Grandchild"
          },
          {
            text: "Grandchild 2"
            type: "Grandchild"
          }
        ]
      },
      {
        text: "Child 2",
        type: "Child"
      }
    ]
  },
  {
    text: "Parent 2",
    type: "Parent"
  },
  {
    text: "Parent 3",
    type: "Parent"
  }
];
我想过滤这棵树。我使用的方法是:

function filter(array, text) {
    return array.filter(function iter(o) {
        var temp;
        if (o.text === text) {
            return true;
        }
        if (!Array.isArray(o.nodes)) {
            return false;
        }
        temp = o.nodes.filter(iter);
        if (temp.length) {
            o.nodes = temp;
            return true;
        }
    });
}
它工作得非常好。这个问题以前就有人问过

尽管如此,我有一个用例,即如果仅在类型为孙子的情况下匹配孙子,而不在子文本或父文本中匹配孙子,那么兄弟节点也应该与其各自的父节点一起返回。答案可能很简单,但不知何故我无法思考如何做到这一点

所以如果我通过这个:孙子2 预期产出应为:

[
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        type: "Child",
        nodes: [
          {
            text: "Grandchild 1"
            type: "Grandchild"
          },
          {
            text: "Grandchild 2"
            type: "Grandchild"
          }
        ]
      }
    ]
  }
]

任何帮助都将不胜感激。谢谢

Write helper方法,首先查找包含给定了grand child文本的条件的对象,然后使用节点上的筛选器更新找到的对象

常量过滤器=数据,字=>{ const criteria=x=>x.nodes?.find{text}=>text==word; const found=data.finditem=>{ if!item.nodes{ 返回false; } 返回item.nodes.findcriteria; }; 返回找到的?[{…找到的,节点:found.nodes.filtercriteria}]:null; }; 变量树=[ { 文本:父1, 节点:[ { 文本:儿童1, 类型:儿童, 节点:[ { 文本:孙女1, 类型:孙女 }, { 文本:孙女2, 类型:孙女 } ] }, { 文本:儿童2, 类型:儿童 } ] }, { 文本:父2, 类型:父级 }, { 文本:父3, 类型:父级 } ];
console.logfiltertree,孙子2 Write helper方法首先查找包含给定grand child文本条件的对象,然后使用节点上的筛选器更新找到的对象

常量过滤器=数据,字=>{ const criteria=x=>x.nodes?.find{text}=>text==word; const found=data.finditem=>{ if!item.nodes{ 返回false; } 返回item.nodes.findcriteria; }; 返回找到的?[{…找到的,节点:found.nodes.filtercriteria}]:null; }; 变量树=[ { 文本:父1, 节点:[ { 文本:儿童1, 类型:儿童, 节点:[ { 文本:孙女1, 类型:孙女 }, { 文本:孙女2, 类型:孙女 } ] }, { 文本:儿童2, 类型:儿童 } ] }, { 文本:父2, 类型:父级 }, { 文本:父3, 类型:父级 } ];
console.logfiltertree,孙子2 如果想要的对象在数组中,则可以采用非变异方法,然后从数组中获取所有项,否则获取下一级,如果子级在下一级,则添加对象

函数筛选器数组、键、值{ return array.someo=>o[key]==value&&o.type===='sunder' 大堆 :array.reducer,o=>{ 如果o[键]==值返回[…r,o]; 如果是o节点{ const nodes=filtero.nodes,key,value; 如果nodes.length r.push{…o,nodes}; } 返回r; }, []; } var-tree=[{text:Parent 1,节点:[{text:Child 1,type:Child,节点:[{text:granten 1,type:granten},{text:granten 2,type:granten}]},{text:Child 2,type:Child}]},{text:Parent 2,type:Parent},{text:Parent 3,type:Parent}]; console.logfiltertree,'text','孙子2';
.as console wrapper{max height:100%!important;top:0;}如果想要的对象在数组中,则可以采用非变异的方法,然后从数组中获取所有项,否则获取下一级,如果子级在下一级,则添加该对象

函数筛选器数组、键、值{ return array.someo=>o[key]==value&&o.type===='sunder' 大堆 :array.reducer,o=>{ 如果o[键]==值返回[…r,o]; 如果是o节点{ const nodes=filtero.nodes,key,value; 如果nodes.length r.push{…o,nodes}; } 返回r; }, []; } var-tree=[{text:Parent 1,节点:[{text:Child 1,type:Child,节点:[{text:granten 1,type:granten},{text:granten 2,type:granten}]},{text:Child 2,type:Child}]},{text:Parent 2,type:Parent},{text:Parent 3,type:Parent}]; console.logfiltertree,'text','孙子2';
.作为控制台包装器{max height:100%!important;top:0;}Hi@NinaScholz,很抱歉,这被延迟了。我在问题中已经提到,只有在类型为孙子的情况下才应返回所有同级节点。但在答案中,如果我提供父对象1,所有对象都将返回。你能看看这个吗。谢谢嗨@NinaScholz,抱歉耽搁了。我在问题中提到过 n本身,所有同级节点只应在类型为孙子的情况下返回。但在答案中,如果我提供父对象1,所有对象都将返回。你能看看这个吗。谢谢