JS 多条件过滤树

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

假设我有以下数据结构:

const tree = {
  id: 'root',
  name: 'Root',
  children: [
    {
      type: 'foo',
      id: 'one',
      name: 'Name 1',
      children: [
        {
          type: 'account',
          id: 'account1',
          name: 'Account 1',
          children: [
            { id: '11', name: 'Asset 1' },
            { id: '22', name: 'Asset 2' }
          ]
        }
      ]
    },
    {
      type: 'bar',
      id: 'two',
      name: 'Name 2',
      children: [
        {
          type: 'sub',
          id: 'sub1',
          name: 'Sub 1',
          children: [
            { id: '11', name: 'Asset 1' },
            { id: '44', name: 'Asset 4' }
          ]
        }
      ]
    }
  ]
};

我需要使用 AND 添加多个过滤器支持,基于

type
accounId
assetName
subId

例如,如果我有以下过滤器:

{
  assetName: 'Asset 1'
}

我期望只得到相关的节点。

如果我有以下过滤器:

{
  assetName: 'Asset 1',
  type: 'bar'
}

我期望只得到第二个节点。

我怎样才能做到这一点?我只知道如何使用一个过滤器。

javascript algorithm tree
© www.soinside.com 2019 - 2024. All rights reserved.