• JS: 数组和树的相互转换


    一、数组转树

    1. let arr = [{
    2. "categoryCode": "90000",
    3. "categoryCnName": "A1级别",
    4. "bdictCode": "111",
    5. "bdictDesc": "测试1",
    6. "bparentCode": "0",
    7. "validStatus": "1"
    8. }, {
    9. "categoryCode": "90000",
    10. "categoryCnName": "A1级别",
    11. "bdictCode": "222",
    12. "bdictDesc": "测试2",
    13. "bparentCode": "0",
    14. "validStatus": "1"
    15. }, {
    16. "categoryCode": "90000",
    17. "categoryCnName": "A1级别",
    18. "bdictCode": "333",
    19. "bdictDesc": "测试3",
    20. "bparentCode": "0",
    21. "validStatus": "1"
    22. }, {
    23. "categoryCode": "90000",
    24. "categoryCnName": "A1级别",
    25. "bdictCode": "444",
    26. "bdictDesc": "测试4",
    27. "bparentCode": "0",
    28. "validStatus": "1"
    29. }, {
    30. "categoryCode": "90000",
    31. "categoryCnName": "A1级别",
    32. "bdictCode": "555",
    33. "bdictDesc": "测试5",
    34. "bparentCode": "0",
    35. "validStatus": "1"
    36. }, {
    37. "categoryCode": "90000",
    38. "categoryCnName": "A1级别",
    39. "bdictCode": "DOCA",
    40. "bdictDesc": "测试6",
    41. "bparentCode": "0",
    42. "validStatus": "1"
    43. }, {
    44. "categoryCode": "90000",
    45. "categoryCnName": "A1级别",
    46. "bdictCode": "DOCA01",
    47. "bdictDesc": "测试7",
    48. "bparentCode": "DOCA",
    49. "validStatus": "1"
    50. }, {
    51. "categoryCode": "90000",
    52. "categoryCnName": "A1级别",
    53. "bdictCode": "DOCA0101",
    54. "bdictDesc": "测试8",
    55. "bparentCode": "DOCA01",
    56. "validStatus": "1"
    57. }, {
    58. "categoryCode": "90000",
    59. "categoryCnName": "A1级别",
    60. "bdictCode": "DOCA0102",
    61. "bdictDesc": "测试9",
    62. "bparentCode": "DOCA01",
    63. "validStatus": "1"
    64. }, {
    65. "categoryCode": "90000",
    66. "categoryCnName": "A1级别",
    67. "bdictCode": "DOCA0103",
    68. "bdictDesc": "测试10",
    69. "bparentCode": "DOCA01",
    70. "validStatus": "1"
    71. }, {
    72. "categoryCode": "90000",
    73. "categoryCnName": "A1级别",
    74. "bdictCode": "DOCA02",
    75. "bdictDesc": "测试11",
    76. "bparentCode": "DOCA",
    77. "validStatus": "1"
    78. }, {
    79. "categoryCode": "90000",
    80. "categoryCnName": "A1级别",
    81. "bdictCode": "DOCA03",
    82. "bdictDesc": "测试12",
    83. "bparentCode": "DOCA",
    84. "validStatus": "1"
    85. }, {
    86. "categoryCode": "90000",
    87. "categoryCnName": "A1级别",
    88. "bdictCode": "DOCA0301",
    89. "bdictDesc": "测试13",
    90. "bparentCode": "DOCA03",
    91. "validStatus": "1"
    92. }, {
    93. "categoryCode": "90000",
    94. "categoryCnName": "A1级别",
    95. "bdictCode": "DOCA0302",
    96. "bdictDesc": "测试14",
    97. "bparentCode": "DOCA03",
    98. "validStatus": "1"
    99. }, {
    100. "categoryCode": "90000",
    101. "categoryCnName": "A1级别",
    102. "bdictCode": "DOCA04",
    103. "bdictDesc": "测试15",
    104. "bparentCode": "DOCA",
    105. "validStatus": "1"
    106. }, {
    107. "categoryCode": "90000",
    108. "categoryCnName": "A1级别",
    109. "bdictCode": "DOCA05",
    110. "bdictDesc": "测试16",
    111. "bparentCode": "DOCA",
    112. "validStatus": "1"
    113. }, {
    114. "categoryCode": "90000",
    115. "categoryCnName": "A1级别",
    116. "bdictCode": "DOCA06",
    117. "bdictDesc": "测试17",
    118. "bparentCode": "DOCA",
    119. "validStatus": "1"
    120. }, {
    121. "categoryCode": "90000",
    122. "categoryCnName": "A1级别",
    123. "bdictCode": "DOCA07",
    124. "bdictDesc": "测试18",
    125. "bparentCode": "DOCA",
    126. "validStatus": "1"
    127. }, {
    128. "categoryCode": "90000",
    129. "categoryCnName": "A1级别",
    130. "bdictCode": "DOCA08",
    131. "bdictDesc": "测试19",
    132. "bparentCode": "DOCA",
    133. "validStatus": "1"
    134. }, {
    135. "categoryCode": "90000",
    136. "categoryCnName": "A1级别",
    137. "bdictCode": "DOCA09",
    138. "bdictDesc": "测试20",
    139. "bparentCode": "DOCA",
    140. "validStatus": "1"
    141. }, {
    142. "categoryCode": "90000",
    143. "categoryCnName": "A1级别",
    144. "bdictCode": "DOCA10",
    145. "bdictDesc": "测试21",
    146. "bparentCode": "DOCA",
    147. "validStatus": "1"
    148. }, ]
    149. function arrayToTree(list, parentID) {
    150. const child = function(pareID) {
    151. //先定义一个数组,用于存储所查到的子元素
    152. const childs = [];
    153. //循环数组
    154. for (let i = 0; i < list.length; i++) {
    155. //如果数组其中一项的bparentCode等于传入的,说明这一项是传入的子元素,把他push进数组,然后重复递归自己找该项的子元素
    156. if (list[i].bparentCode === pareID) {
    157. if (child(list[i].bdictCode).length > 0) {
    158. list[i].children = child(list[i].bdictCode);
    159. }
    160. childs.push(list[i]);
    161. }
    162. }
    163. //最后将查到的所有子元素返回
    164. return childs;
    165. };
    166. return child(parentID);
    167. }
    168. let res = arrayToTree(arr, '0')
    169. console.log(res);

    二、树转数组

    树转数组,实现的要点还是在于递归查找,定义一个空数组,去接收没有children属性的每一项

    1. let treeRes = [];
    2. function tree2arr(res) {
    3. res.forEach(item => {
    4. if (!item.children) {
    5. treeRes.push(item)
    6. } else {
    7. tree2arr(item.children)
    8. }
    9. })
    10. }
    11. // 此处的res取的是上边的数组转树的结果
    12. tree2arr(res)
    13. console.log(treeRes);

  • 相关阅读:
    Vuforia AR篇(三)— AR模型出场效果
    解决Python requests库中的重定向问题
    通过starrocks jdbc外表查询sqlserver
    又一单SoC行泊一体域控发布!这家厂商将性能和成本“卷”到极致
    Rust 二叉树遍历---- DFS 和 BFS
    `include指令【FPGA】
    D-star Lite算法及相关思考
    算法作业1-2 字典序问题
    LeetCode301:删除无效的括号
    nvidia-smi详解
  • 原文地址:https://blog.csdn.net/m0_47135993/article/details/125957048