Skip to content

filterTree

过滤树节点

示例

js
const output = filterTree(input, node => node.id === '1-1-1', { childrenKey: 'children' })
js
// input:
[
  {
    id: '1',
    parentId: '0',
    children: [
      {
        id: '1-1',
        parentId: '1',
        children: [{ id: '1-1-1', parentId: '1-1' }]
      },
      {
        id: '1-2',
        parentId: '1',
        children: [
          { id: '1-2-1', parentId: '1-2' },
          { id: '1-2-2', parentId: '1-2' }
        ]
      }
    ]
  }
];

// output:
[
  {
    id: '1-1-1',
    parentId: '1-1'
  }
]

参数

名称说明类型可选值默认值
tree树数据Array--
fn过滤函数(node) => boolean--
options参数配置FilterTreeOptions--

FilterTreeOptions

名称说明类型可选值默认值
childrenKey子节点的名string-"children"
flat是否扁平化boolean-false

返回值

(Array): 返回过滤出的节点数组