如何有效地过滤树形视图并保留其现有结构?

14

我有一个树形结构的JSON,需要进行过滤,并且结果应该保留树形结构。

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"
  }
];

例子:

1) 如果搜索查询是 Parent 1

期望结果:

[
  {
    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"
      }
    ]
  }
]

2) 如果搜索查询为 Child 1

期望结果:

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

3) 如果搜索查询为Grandchild 2

期望结果:

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

我需要保留基于节点级别的树形结构(在这里指类型)。迄今为止,我已经尝试了递归过滤,但无法重新映射结果。

angular.module("myApp",[])
   .filter("filterTree",function(){
       return function(items,id){
          var filtered = [];
          var recursiveFilter = function(items,id){
              angular.forEach(items,function(item){
                 if(item.text.toLowerCase().indexOf(id)!=-1){
                    filtered.push(item);
                 }
                 if(angular.isArray(item.items) && item.items.length > 0){
                    recursiveFilter(item.items,id);              
                 }
              });
          };
          recursiveFilter(items,id);
          return filtered;
       }; 
    });
});

我的JSON相当大,因此基于类型的重新映射预计将在过滤器本身中完成。请给予建议。

1个回答

51
你可以使用嵌套递归方法并过滤树形结构,同时保持发现的项目。此解决方案不会改变原始数据。

function filter(array, text) {
    const getNodes = (result, object) => {
        if (object.text === text) {
            result.push(object);
            return result;
        }
        if (Array.isArray(object.nodes)) {
            const nodes = object.nodes.reduce(getNodes, []);
            if (nodes.length) result.push({ ...object, nodes });
        }
        return result;
    };

    return array.reduce(getNodes, []);
}

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" }];

console.log(filter(tree, 'Parent 1'));
console.log(filter(tree, 'Child 1'));
console.log(filter(tree, 'Grandchild 2'));
.as-console-wrapper { max-height: 100% !important; top: 0; }


谢谢!这个答案救了我。 - Dungeon
4
非常好,我在阅读突变副作用之前花了三个小时……非常感谢。 - Rigoberta Raviolini
很棒的解决方案! - Daniel S.
如果在示例1中没有子节点包含文本“Parent 1”,我该如何显示空节点? - user3106347
@user3106347,请提出一个新问题。 - Nina Scholz
我会给你想要的任何饮料作为这项工作的报酬!这节省了我更长时间的工作!谢谢!如果我能投更多票就好了! - nickanor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接