如何在树中遍历并过滤javascript中的匹配节点?

问题描述 投票:1回答:1

我试图过滤节点,但成功只过滤根节点。我的实现如下。如何使用方法过滤子节点?

  function getMatchedValues(nodes)
    {
        var leafContainsText = false;

        for (var i = 0; i < nodes.length; i++)
        {
            if (nodes[i].items && nodes[i].items.length > 0)
            {
                leafContainsText = getMatchedValues(nodes[i].items);
                if (leafContainsText)
                    break;
            }
            else
            {
                if (nodes[i].text.toLowerCase().indexOf($scope.filterReports.toLowerCase()) !== -1)
                {
                    leafContainsText = true;
                    break;
                }                        
            }
        }

        return leafContainsText;
    }


 $scope.temp_reports = [];

        for (var i = 0; i < $scope.reports.length; i++)
        {
            if ($scope.reports[i].items && $scope.reports[i].items.length > 0)
            {
                if (getMatchedValues($scope.reports[i].items))
                    $scope.temp_reports.push($scope.reports[i]);
                else if ($scope.reports[i].text.toLowerCase().indexOf($scope.filterReports.toLowerCase()) !== -1)
                    $scope.temp_reports.push($scope.reports[i]);
            }
            else
            {
                if ($scope.reports[i].text.toLowerCase().indexOf($scope.filterReports.toLowerCase()) !== -1)
                    $scope.temp_reports.push($scope.reports[i]);
            }
        }

树对象($ scope.reports)如下所示:

     [
       {
          text:"abc",
          items:[
             {
                text:"abf"
             },
             {
                text:"abd",
                items:[
                   {
                      text:"bbb"
                   },
                   {
                      text:"dba"
                   }
                ]
             }
          ]
       },
       {
          text:"def",
          items:[
             {
                text:"ddf"
             },
             {
                text:"ddd",
                items:[
                   {
                      text:"dfg"
                   },
                   {
                      text:"dba",
                      items:[
                         {
                            text:"qqq"
                         },
                         {
                            text:"www"
                         }
                      ]
                   }
                ]
             }
          ]
       }
    ]

例如,如果要过滤包含“d”的节点,则结果树应如下所示;

         [
       {
          text:"abc",
          items:[
             {
                text:"abd",
                items:[
                   {
                      text:"dba"
                   }
                ]
             }
          ]
       },
       {
          text:"def",
          items:[
             {
                text:"ddf"
             },
             {
                text:"ddd",
                items:[
                   {
                      text:"dfg"
                   },
                   {
                      text:"dba",
                      items:[]
                   }
                ]
             }
          ]
       }
    ]
javascript iteration
1个回答
1
投票

您也可以通过过滤items数组来过滤数组。

该解决方案改变了原始数据。

function filter(array, search) {
    return array.filter(function f(o) {
        var t;

        if (o.items) {
            t = o.items.filter(f);
        }
        if (o.text.includes(search) || t && t.length) {
            if (t) {
                o.items = t;
            }
            return true;
        }
    });
}

var array = [{ text: "abc", items: [{ text: "abf" }, { text: "abd", items: [{ text: "bbb" }, { text: "dba" }] }] }, { text: "def", items: [{ text: "ddf" }, { text: "ddd", items: [{ text: "dfg" }, { text: "dba", items: [{ text: "qqq" }, { text: "www" }] }] }] }],
    result = filter(array, 'd');

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
© www.soinside.com 2019 - 2024. All rights reserved.