Skip to content

findTree

js
findTree(data, value, callback, fieldNames)

从树型结构中查找符合条件的数据

参数

名称说明类型默认值
data数据列表array-
value父级初始值number string object0
callback回调函数function-
fieldNames字段名object{ key: 'id', children: 'children' }

代码演示

基础用法

js
const data = [
    {
        "id": "1",
        "title": "选项1",
        "parentId": "0",
        "children": [
            {
                "id": "1-1",
                "title": "选项1-1",
                "parentId": "1"
            },
            {
                "id": "1-2",
                "title": "选项1-2",
                "parentId": "1"
            }
        ]
    },
    {
        "id": "2",
        "title": "选项2",
        "parentId": "0",
        "children": [
            {
                "id": "2-1",
                "title": "选项2-1",
                "parentId": "2"
            },
            {
                "id": "2-2",
                "title": "选项2-2",
                "parentId": "2"
            }
        ]
    }
]

findTree(data, '2-1', (item, index, array, path) => {
    console.log(item)
    console.log(index)
    console.log(array)
    console.log(path)
})
json
{
  "id": "2-1",
  "title": "选项2-1",
  "parentId": "2"
}
json
0
json
[
  {
    "id": "2-1",
    "title": "选项2-1",
    "parentId": "2"
  },
  {
    "id": "2-2",
    "title": "选项2-2",
    "parentId": "2"
  }
]
json
[
  {
    "id": "2",
    "title": "选项2",
    "parentId": "0",
    "children": [
      {
        "id": "2-1",
        "title": "选项2-1",
        "parentId": "2"
      },
      {
        "id": "2-2",
        "title": "选项2-2",
        "parentId": "2"
      }
    ]
  },
  {
    "id": "2-1",
    "title": "选项2-1",
    "parentId": "2"
  }
]

使用多个字段

js
const data = [
    {
        "id": "1",
        "title": "选项1",
        "parentId": "0",
        "active": false,
        "children": [
            {
                "id": "1-1",
                "title": "选项1-1",
                "parentId": "1",
                "active": false,
            },
            {
                "id": "1-2",
                "title": "选项1-2",
                "parentId": "1",
                "active": false,
            }
        ]
    },
    {
        "id": "2",
        "title": "选项2",
        "parentId": "0",
        "active": false,
        "children": [
            {
                "id": "2-1",
                "title": "选项2-1",
                "parentId": "2",
                "active": false,
            },
            {
                "id": "2-2",
                "title": "选项2-2",
                "parentId": "2",
                "active": true,
            }
        ]
    }
]

findTree(data, { parentId: '2', active: true }, (item, index, array, path) => {
    console.log(item)
    console.log(index)
    console.log(array)
    console.log(path)
})
json
{
  "id": "2-2",
  "title": "选项2-2",
  "parentId": "2",
  "active": true
}
json
1
json
[
  {
    "id": "2-1",
    "title": "选项2-1",
    "parentId": "2",
    "active": false
  },
  {
    "id": "2-2",
    "title": "选项2-2",
    "parentId": "2",
    "active": true
  }
]
json
[
  {
    "id": "2",
    "title": "选项2",
    "parentId": "0",
    "active": false,
    "children": [
      {
        "id": "2-1",
        "title": "选项2-1",
        "parentId": "2",
        "active": false
      },
      {
        "id": "2-2",
        "title": "选项2-2",
        "parentId": "2",
        "active": true
      }
    ]
  },
  {
    "id": "2-2",
    "title": "选项2-2",
    "parentId": "2",
    "active": true
  }
]

本文档内容版权为 XYAdmin 作者所有,保留所有权利。