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

时间:2018-03-05 09:49:41

标签: javascript iteration

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

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

例如,如果要过滤包含&#39; d&#39;那么结果树应该是这样的;

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

1 个答案:

答案 0 :(得分:1)

您也可以通过过滤SELECT DISTINCT IIf( ( InStr( InStr([Name],' ') + 1 , [Name], ' ') > 0 ), Left( [Name], InStr(InStr([Name],' ') + 1 , [Name], ' ') ), [Name]) FROM MyTable; 数组来过滤数组。

此解决方案会改变原始数据。

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);