Skip to content

findTree

查找树节点

示例

js
const output = findTree(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参数配置FindTreeOptions--

FindTreeOptions

名称说明类型可选值默认值
childrenKey子节点的名string-"children"
returnChildren是否返回子节点boolean-false

返回值

(Object): 返回查找到的节点