需求说明:当一个节点值为特定值的时候,改变该节点下另外一个值。

    实现思路如下:
    代码如下实现:

    1. //报文结构
    2. var arr = [{
    3. id: 1,
    4. pid: 0,
    5. name: "大良造菜单",
    6. open: true,
    7. nocheck: false,
    8. children: [{
    9. id: 11,
    10. pid: 1,
    11. name: "当前项目"
    12. },
    13. {
    14. id: 12,
    15. pid: 1,
    16. name: "工程管理",
    17. open: true,
    18. children: [{
    19. id: 121,
    20. pid: 12,
    21. name: "我的工程"
    22. },
    23. {
    24. id: 122,
    25. pid: 12,
    26. name: "施工调度"
    27. },
    28. {
    29. id: 1211,
    30. pid: 12,
    31. name: "材料竞价"
    32. }
    33. ]
    34. },
    35. {
    36. id: 13,
    37. pid: 1,
    38. name: "录入管理",
    39. open: true,
    40. children: [{
    41. id: 131,
    42. pid: 12,
    43. name: "用工录入",
    44. children: [],
    45. isParent: false
    46. },
    47. {
    48. id: 132,
    49. pid: 12,
    50. name: "商家录入"
    51. },
    52. {
    53. id: 1314,
    54. pid: 12,
    55. name: "机构列表"
    56. }
    57. ]
    58. },
    59. {
    60. id: 14,
    61. pid: 1,
    62. name: "审核管理",
    63. open: true,
    64. children: [{
    65. id: 141,
    66. pid: 14,
    67. name: "用工审核"
    68. },
    69. {
    70. id: 142,
    71. pid: 14,
    72. name: "商家审核"
    73. },
    74. {
    75. id: 145,
    76. pid: 14,
    77. name: "机构审核"
    78. }
    79. ]
    80. },
    81. {
    82. id: 15,
    83. pid: 1,
    84. name: "公司管理",
    85. open: false,
    86. children: [{
    87. id: 1517,
    88. pid: 15,
    89. name: "我的工程案例"
    90. },
    91. {
    92. id: 1518,
    93. pid: 15,
    94. name: "联系人设置"
    95. },
    96. {
    97. id: 1519,
    98. pid: 15,
    99. name: "广告设置"
    100. }
    101. ]
    102. },
    103. {
    104. id: 16,
    105. pid: 1,
    106. name: "业务管理",
    107. open: true,
    108. children: [{
    109. id: 164,
    110. pid: 16,
    111. name: "合同范本"
    112. },
    113. {
    114. id: 165,
    115. pid: 16,
    116. name: "合同列表"
    117. },
    118. {
    119. id: 166,
    120. pid: 16,
    121. name: "需求调度"
    122. }
    123. ]
    124. },
    125. {
    126. id: 17,
    127. pid: "x",
    128. name: "订单管理",
    129. open: true,
    130. children: [{
    131. id: 171,
    132. pid: 17,
    133. name: "商品订单"
    134. },
    135. {
    136. id: 172,
    137. pid: 17,
    138. name: "用工订单"
    139. },
    140. {
    141. id: 175,
    142. pid: 17,
    143. name: "供应菜单"
    144. }
    145. ]
    146. }
    147. ]
    148. }];
    149. function treeForeach(tree, func) {
    150. tree.map(data => {
    151. func(data)
    152. data.children && treeForeach(data.children, func) // 遍历子树
    153. })
    154. }
    155. treeForeach(arr, node => {
    156. if (node.pid == 'x'){
    157. node.name = "666";
    158. }
    159. console.log(arr);
    160. // console.log(arr);
    161. })

    整体代码偏简练,不断的调用子树,知道最深层结构为止。