js_数组转树型结构

将平面数组结构,根据关联属性转成树型结构

源数据(平面)

              
  • 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
[{ "third_party_dept_id": "2", "dept_name": "全体教师", "parent_dept_id": "1" }, { "third_party_dept_id": "3", "dept_name": "学生成长", "parent_dept_id": "1" }, { "third_party_dept_id": "4", "dept_name": "初一年级", "parent_dept_id": "3" }, { "third_party_dept_id": "5", "dept_name": "初一(1)班", "parent_dept_id": "4" }, { "third_party_dept_id": "6", "dept_name": "初一(2)班", "parent_dept_id": "4" }, { "third_party_dept_id": "7", "dept_name": "初二年级", "parent_dept_id": "3" }, { "third_party_dept_id": "8", "dept_name": "初二(1)班", "parent_dept_id": "7" }, { "third_party_dept_id": "9", "dept_name": "初二(1-2)班", "parent_dept_id": "8" }]

转成树形结构,如下

              
  • 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
[ { "third_party_dept_id":"2", "dept_name":"全体教师", "children":[ ] }, { "third_party_dept_id":"3", "dept_name":"学生成长", "children":[ { "third_party_dept_id":"4", "dept_name":"初一年级", "children":[ { "third_party_dept_id":"5", "dept_name":"初一(1)班", "children":[ ] }, { "third_party_dept_id":"6", "dept_name":"初一(2)班", "children":[ ] } ] }, { "third_party_dept_id":"7", "dept_name":"初二年级", "children":[ { "third_party_dept_id":"8", "dept_name":"初二(1)班", "children":[ { "third_party_dept_id":"9", "dept_name":"初二(1-2)班", "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
/** * * @param [{ "third_party_dept_id": "2", "dept_name": "全体教师", "parent_dept_id": "1" }, { "third_party_dept_id": "3", "dept_name": "学生成长", "parent_dept_id": "1" }, { "third_party_dept_id": "4", "dept_name": "初一年级", "parent_dept_id": "3" }, { "third_party_dept_id": "5", "dept_name": "初一(1)班", "parent_dept_id": "4" }] data * @param { id: 'third_party_dept_id', parentId: 'parent_dept_id', name: 'dept_name', rootId: 1 } param */ // 数组转树型结构 function toTreeData(dataArr, param) { var treeArr = []; for (var i = 0; i < dataArr.length; i++) { if (dataArr[i][param.parentId] == param.rootId) { var obj = {}; obj[param.id] = dataArr[i][param.id]; obj[param.name] = dataArr[i][param.name]; obj.children = []; treeArr.push(obj); dataArr.splice(i, 1); i--; } } function createChildren(childArr) { if (dataArr.length !== 0) { for (var i = 0; i < childArr.length; i++) { for (var j = 0; j < dataArr.length; j++) { if (childArr[i][param.id] == dataArr[j][param.parentId]) { var obj = {}; obj[param.id] = dataArr[j][param.id]; obj[param.name] = dataArr[j][param.name]; obj.children = []; childArr[i].children.push(obj); dataArr.splice(j, 1); j--; } } createChildren(childArr[i].children); } } } createChildren(treeArr); return treeArr; }

此处应有案例: 点击跳转

满意就赞助一下吧 😃😃😃

本文于 2018/7/12 下午 发布在 编程 分类下,当前已被围观 1663 次

相关标签:js

永久地址:https://blog.pandashuai.com/article/22