sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

    • 序言 👏
    • uniq 【数组去重】
    • arrayToMap 【数组转成Map】
    • arrayToObj 【数组转成obj】
    • chunk 【数组切割】
    • compact 【去除数组中无效值】
    • remove 【数组中移除一个元素】
    • shuffle 【数组随机打乱】
    • groupBy 【分组】
    • compactIsNoNullable 【去除数组中null/undefined/NaN】
    • arrayDiff【比较数组差异】🔥🔥
    • differenceBy【获取新增数组】🔥
    • intersection 【数组交集】
    • matchSubListGroup
    • listToTree【列表转树形】🔥🔥🔥
    • treeToList【树型转列表】🔥🔥🔥
    • eachTree【遍历树形结构】🔥🔥🔥
    • filterTree【过滤树形结构】🔥🔥🔥
    • childNodesInList【列表中获取某个下所有后代节点】🔥
    • parentNodesInTree【tree中获取所有祖先节点】 🔥
      • 1.示例
      • 2.入参说明
        • 主入参
        • props 对象
      • 3.源码
    • formatStrategyIdsInTree【树形数据中只选中父节点】 🔥
    • range 【序号列表生成函数】
  • 方法-Function

  • 字符串-String

  • 数学-Math

  • 文件-Buffer

  • 节点-dom

  • 拓展

  • nodejs

目录

parentNodesInTree【tree中获取所有祖先节点】 🔥

描述

根据树状子节点 id, 依次找到所有父节点路径(递归处理) 常用

# 1.示例

import { parentNodesInTree } from 'sf-utils2'

const tree = [
  {
    id: 1,
    name: '香蕉',
    pId: null,
    children: [
      {
        id: 1001,
        name: '香蕉A',
        pId: 1,
        children: [
          {
            id: 1001001,
            name: '香蕉A-儿子',
            pId: 1001,
            children: [
              {
                id: 1001001001,
                name: '香蕉A-儿子-儿子',
                pId: 1001001
              }
            ]
          }
        ]
      },
      {
        id: 1002,
        name: '香蕉B',
        pId: 1,
        children: [
          {
            id: 1002002,
            name: '香蕉B-儿子',
            pId: 1002
          }
        ]
      },
      {
        id: 1003,
        name: '香蕉C',
        pId: 1,
        children: [
          {
            id: 1003003,
            name: '香蕉C-儿子',
            pId: 1003
          }
        ]
      }
    ]
  },
  {
    id: 2,
    name: '苹果',
    pId: null,
    children: [
      {
        id: 1004,
        name: '苹果A',
        pId: 2,
        children: [
          {
            id: 1004004,
            name: '苹果A-儿子',
            pId: 1004
          }
        ]
      },
      {
        id: 1007,
        name: '苹果D',
        pId: 2
      },
      {
        id: 1008,
        name: '苹果D',
        pId: 2
      },
      {
        id: 1009,
        name: '苹果C',
        pId: 2
      }
    ]
  },
  {
    id: 3,
    name: '橘子',
    pId: null,
    children: [
      {
        id: 1005,
        name: '橘子C',
        pId: 3,
        children: [
          {
            id: 1005005,
            name: '橘子C-儿子',
            pId: 1005
          }
        ]
      },
      {
        id: 1006,
        name: '橘子B',
        pId: 3,
        children: [
          {
            id: 1006006,
            name: '橘子B-儿子',
            pId: 1006
          }
        ]
      },
      {
        id: 1010,
        name: '橘子A',
        pId: 3
      }
    ]
  },
  {
    id: 4,
    name: '西瓜',
    pId: null,
    children: [
      {
        id: 1012,
        name: '西瓜B',
        pId: 4,
        children: [
          {
            id: 1013,
            name: '西瓜B-儿子',
            pId: 1012
          }
        ]
      }
    ]
  },
  {
    id: 1011,
    name: '西瓜A',
    pId: null
  }
]

// 根据id是1006006,依次找其上面的 父节点
console.log(parentNodesInTree({ tree: tree, id: 1006006, props: { id: 'id', children: 'children' } }))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
输出结果,点开查看 👇
;[
  {
    id: 3,
    name: '橘子',
    pId: null,
    children: [
      {
        id: 1005,
        name: '橘子C',
        pId: 3,
        children: [
          {
            id: 1005005,
            name: '橘子C-儿子',
            pId: 1005
          }
        ]
      },
      {
        id: 1006,
        name: '橘子B',
        pId: 3,
        children: [
          {
            id: 1006006,
            name: '橘子B-儿子',
            pId: 1006
          }
        ]
      },
      {
        id: 1010,
        name: '橘子A',
        pId: 3
      }
    ]
  },
  {
    id: 1006,
    name: '橘子B',
    pId: 3,
    children: [
      {
        id: 1006006,
        name: '橘子B-儿子',
        pId: 1006
      }
    ]
  },
  {
    id: 1006006,
    name: '橘子B-儿子',
    pId: 1006
  }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

# 2.入参说明

# 主入参

参数 说明 类型 是否必填 默认值
tree 原数据,树状结构 Array 是
props 自定义键名,详情见下 👇 Object 否
id 要查找的子节点主键值 String
Number
否

# props 对象

参数 说明 类型 是否必填 默认值
id 自身主键名,唯一标识 String 否 id
children 树状 children 键名 String 否 children

# 3.源码

源码,点开查看 👈
import isArray from '@/base/isArray'

/**
 * 根据树状子节点id,依次找到所有父节点路径
 * @param {Array} tree 原数据,树状结构
 * @param {string | number} id 要查找的子节点主键值
 * @param {object} props 自定义键名
 * @returns {*[]}
 */
function parentNodesInTree({ tree = [], id = '', props = { id: 'id', children: 'children' } }) {
  if (!isArray(tree)) return []
  const parentNodes = []
  const getNodePath = (node, id = '', props) => {
    parentNodes.push(node)
    if (node?.[props.id] == id) {
      throw 'break'
    }
    if (isArray(node?.[props.children])) {
      for (let i = 0; i < node?.[props.children].length; i++) {
        getNodePath(node[props.children][i], id, props)
      }
    }
    parentNodes.pop()
  }
  try {
    for (let i = 0; i < tree.length; i++) {
      getNodePath(tree[i], id, props)
    }
  } catch (e) {
    return parentNodes
  }
}

export default parentNodesInTree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
上次更新: 2023/06/24, 19:35:48
childNodesInList【列表中获取某个下所有后代节点】🔥
formatStrategyIdsInTree【树形数据中只选中父节点】 🔥

← childNodesInList【列表中获取某个下所有后代节点】🔥 formatStrategyIdsInTree【树形数据中只选中父节点】 🔥→

Theme by Vdoing | Copyright © 2022-2025 bianpengfei
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×