let obj = { "data": [{ "id": 2, "code": "10010000", "name": "涉政", "parentCode": "10000000", "level": 2, "sort": 1, "createTime": "2020-09-17T18:06:05", "updateTime": "2020-11-25T15:42:11" }, { "id": 11, "code": "10020000", "name": "涉恐", "parentCode": "10000000", "level": 2, "sort": 2, "createTime": "2020-09-20T11:36:57", "updateTime": "2020-11-25T15:42:11" }, { "id": 12, "code": "10030000", "name": "涉邪教", "parentCode": "10000000", "level": 2, "sort": 3, "createTime": "2020-09-20T11:36:58", "updateTime": "2020-11-25T15:42:11" }, { "id": 4, "code": "20010000", "name": "涉众", "parentCode": "20000000", "level": 2, "sort": 5, "createTime": "2020-09-17T18:11:34", "updateTime": "2020-11-25T15:42:11" }, { "id": 8, "code": "20020000", "name": "涉军", "parentCode": "20000000", "level": 2, "sort": 4, "createTime": "2020-09-20T11:36:57", "updateTime": "2020-11-25T15:42:11" }, { "id": 9, "code": "20030000", "name": "小区业主", "parentCode": "20000000", "level": 2, "sort": 6, "createTime": "2020-09-20T11:36:57", "updateTime": "2020-11-25T15:42:11" }, { "id": 10, "code": "20040000", "name": "其他利益群体", "parentCode": "20000000", "level": 2, "sort": 7, "createTime": "2020-09-20T11:36:57", "updateTime": "2020-11-25T15:42:11" }, { "id": 34, "code": "20040100", "name": "传统利益受损群体", "parentCode": "20040000", "level": 3, "sort": 26, "createTime": "2020-09-28T11:05:51", "updateTime": "2020-11-25T15:44:54" }, { "id": 75, "code": "20021300", "name": "两参(参战参核)", "parentCode": "20020000", "level": 3, "sort": 9, "createTime": "2020-09-28T11:05:50", "updateTime": "2020-11-25T15:44:54" }, { "id": 42, "code": "20040102", "name": "民师(中师、民代幼)", "parentCode": "20040100", "level": 4, "sort": 9, "createTime": "2020-09-28T11:15:16", "updateTime": "2020-11-25T15:48:29" }, { "id": 36, "code": "20040300", "name": "民生领域群体", "parentCode": "20040000", "level": 3, "sort": 28, "createTime": "2020-09-28T11:05:51", "updateTime": "2020-11-25T15:44:54" }, { "id": 41, "code": "20040101", "name": "艾滋病", "parentCode": "20040100", "level": 4, "sort": 8, "createTime": "2020-09-28T11:15:16", "updateTime": "2020-11-25T15:48:29" }, { "id": 73, "code": "20021100", "name": "守备二师", "parentCode": "20020000", "level": 3, "sort": 17, "createTime": "2020-09-28T11:05:50", "updateTime": "2020-11-25T15:44:54" }, { "id": 59, "code": "20040302", "name": "医疗卫生类", "parentCode": "20040300", "level": 4, "sort": 25, "createTime": "2020-09-28T11:15:19", "updateTime": "2020-11-25T15:48:29" }, { "id": 44, "code": "20040104", "name": "乡镇机构改革分流人员", "parentCode": "20040100", "level": 4, "sort": 11, "createTime": "2020-09-28T11:15:16", "updateTime": "2020-11-25T15:48:29" }, { "id": 58, "code": "20040301", "name": "教育类", "parentCode": "20040300", "level": 4, "sort": 24, "createTime": "2020-09-28T11:15:19", "updateTime": "2020-11-25T15:48:29" }, { "id": 43, "code": "20040103", "name": "因病致贫", "parentCode": "20040100", "level": 4, "sort": 10, "createTime": "2020-09-28T11:15:16", "updateTime": "2020-11-25T15:48:29" }, { "id": 46, "code": "20040106", "name": "知青群体", "parentCode": "20040100", "level": 4, "sort": 13, "createTime": "2020-09-28T11:15:17", "updateTime": "2020-11-25T15:48:29" }, { "id": 60, "code": "20040303", "name": "就业类", "parentCode": "20040300", "level": 4, "sort": 26, "createTime": "2020-09-28T11:15:19", "updateTime": "2020-11-25T15:48:29" }, { "id": 45, "code": "20040105", "name": "农电工", "parentCode": "20040100", "level": 4, "sort": 12, "createTime": "2020-09-28T11:15:16", "updateTime": "2020-11-25T15:48:29" }, { "id": 48, "code": "20040108", "name": "疫苗受害群体", "parentCode": "20040100", "level": 4, "sort": 15, "createTime": "2020-09-28T11:15:17", "updateTime": "2020-11-25T15:48:29" }, { "id": 47, "code": "20040107", "name": "脑控犯罪受害者", "parentCode": "20040100", "level": 4, "sort": 14, "createTime": "2020-09-28T11:15:17", "updateTime": "2020-11-25T15:48:29" }, { "id": 49, "code": "20040109", "name": "超生开除群体", "parentCode": "20040100", "level": 4, "sort": 16, "createTime": "2020-09-28T11:15:17", "updateTime": "2020-11-25T15:48:29" }, { "id": 18, "code": "10010600", "name": "港澳台领域", "parentCode": "10010000", "level": 3, "sort": 6, "createTime": "2020-09-28T11:05:47", "updateTime": "2020-11-25T15:44:54" }, { "id": 62, "code": "10010202", "name": "境外宗教", "parentCode": "10010200", "level": 4, "sort": 7, "createTime": "2020-09-28T11:18:05", "updateTime": "2020-11-25T15:48:29" }, { "id": 25, "code": "20010600", "name": "其他", "parentCode": "20010000", "level": 3, "sort": 25, "createTime": "2020-09-28T11:05:48", "updateTime": "2020-11-25T15:44:54" }, { "id": 16, "code": "10010400", "name": "高校文保领域", "parentCode": "10010000", "level": 3, "sort": 4, "createTime": "2020-09-28T11:05:47", "updateTime": "2020-11-25T15:44:54" }, { "id": 23, "code": "20010400", "name": "诈骗类", "parentCode": "20010000", "level": 3, "sort": 23, "createTime": "2020-09-28T11:05:48", "updateTime": "2020-11-25T15:44:54" }, { "id": 14, "code": "10010200", "name": "民族宗教领域", "parentCode": "10010000", "level": 3, "sort": 2, "createTime": "2020-09-28T11:05:46", "updateTime": "2020-11-25T15:44:54" }, { "id": 61, "code": "10010201", "name": "境内宗教", "parentCode": "10010200", "level": 4, "sort": 6, "createTime": "2020-09-28T11:18:05", "updateTime": "2020-11-25T15:48:29" }, { "id": 21, "code": "20010200", "name": "线下投资担保", "parentCode": "20010000", "level": 3, "sort": 21, "createTime": "2020-09-28T11:05:48", "updateTime": "2020-11-25T15:44:54" }, { "id": 70, "code": "10010800", "name": "其他", "parentCode": "10010000", "level": 3, "sort": 8, "createTime": "2020-11-24T17:30:12", "updateTime": "2020-11-25T15:46:05" }, { "id": 29, "code": "20020400", "name": "城镇义务兵", "parentCode": "20020000", "level": 3, "sort": 13, "createTime": "2020-09-28T11:05:49", "updateTime": "2020-11-25T15:44:54" }, { "id": 51, "code": "20040111", "name": "乡村医生", "parentCode": "20040100", "level": 4, "sort": 18, "createTime": "2020-09-28T11:15:17", "updateTime": "2020-11-25T15:48:29" }, { "id": 50, "code": "20040110", "name": "失独群体", "parentCode": "20040100", "level": 4, "sort": 17, "createTime": "2020-09-28T11:15:17", "updateTime": "2020-11-25T15:48:29" }, { "id": 69, "code": "30010000", "name": "未知类型", "parentCode": "30000000", "level": 2, "sort": 8, "createTime": "2020-09-17T18:05:37", "updateTime": "2020-11-25T15:42:11" }, { "id": 27, "code": "20020200", "name": "转业志愿兵", "parentCode": "20020000", "level": 3, "sort": 11, "createTime": "2020-09-28T11:05:49", "updateTime": "2020-11-25T15:44:54" }, { "id": 33, "code": "20020800", "name": "其他", "parentCode": "20020000", "level": 3, "sort": 19, "createTime": "2020-09-28T11:05:50", "updateTime": "2020-11-25T15:46:05" }, { "id": 78, "code": "10030100", "name": "法轮功", "parentCode": "10030000", "level": 3, "sort": 1, "createTime": "2020-12-15T16:13:05", "updateTime": "2020-12-15T16:14:05" }, { "id": 80, "code": "10030300", "name": "有害气功", "parentCode": "10030000", "level": 3, "sort": 3, "createTime": "2020-12-15T16:13:05", "updateTime": null }, { "id": 68, "code": "30000000", "name": "未知类型", "parentCode": "-1", "level": 1, "sort": 3, "createTime": "2020-09-17T18:05:37", "updateTime": "2020-11-25T15:41:19" }, { "id": 1, "code": "10000000", "name": "政治安全分类", "parentCode": "-1", "level": 1, "sort": 1, "createTime": "2020-09-17T18:05:37", "updateTime": "2020-11-25T15:41:01" }, { "id": 53, "code": "20040201", "name": "企业改制", "parentCode": "20040200", "level": 4, "sort": 19, "createTime": "2020-09-28T11:15:18", "updateTime": "2020-11-25T15:48:29" }, { "id": 35, "code": "20040200", "name": "社会管理领域群体", "parentCode": "20040000", "level": 3, "sort": 27, "createTime": "2020-09-28T11:05:51", "updateTime": "2020-11-25T15:44:54" }, { "id": 55, "code": "20040203", "name": "环境保护", "parentCode": "20040200", "level": 4, "sort": 21, "createTime": "2020-09-28T11:15:18", "updateTime": "2020-11-25T15:48:29" }, { "id": 77, "code": "20040400", "name": "其他", "parentCode": "20040000", "level": 3, "sort": 29, "createTime": "2020-09-28T11:05:51", "updateTime": "2020-11-25T15:46:05" }, { "id": 74, "code": "20021200", "name": "援藏兵", "parentCode": "20020000", "level": 3, "sort": 18, "createTime": "2020-09-28T11:05:50", "updateTime": "2020-11-25T15:44:54" }, { "id": 54, "code": "20040202", "name": "征地拆迁", "parentCode": "20040200", "level": 4, "sort": 20, "createTime": "2020-09-28T11:15:18", "updateTime": "2020-11-25T15:48:29" }, { "id": 57, "code": "20040205", "name": "工资待遇", "parentCode": "20040200", "level": 4, "sort": 23, "createTime": "2020-09-28T11:15:18", "updateTime": "2020-11-25T15:48:29" }, { "id": 56, "code": "20040204", "name": "移民安置", "parentCode": "20040200", "level": 4, "sort": 22, "createTime": "2020-09-28T11:15:18", "updateTime": "2020-11-25T15:48:29" }, { "id": 67, "code": "10010105", "name": "律师重点人", "parentCode": "10010100", "level": 4, "sort": 5, "createTime": "2020-09-28T11:54:32", "updateTime": "2020-11-25T15:48:29" }, { "id": 19, "code": "10010700", "name": "周边国家", "parentCode": "10010000", "level": 3, "sort": 7, "createTime": "2020-09-28T11:05:48", "updateTime": "2020-11-25T15:44:55" }, { "id": 65, "code": "10010103", "name": "民运", "parentCode": "10010100", "level": 4, "sort": 3, "createTime": "2020-09-28T11:54:32", "updateTime": "2020-11-25T15:48:29" }, { "id": 66, "code": "10010104", "name": "右派", "parentCode": "10010100", "level": 4, "sort": 4, "createTime": "2020-09-28T11:54:32", "updateTime": "2020-11-25T15:48:29" }, { "id": 24, "code": "20010500", "name": "协助稳控类", "parentCode": "20010000", "level": 3, "sort": 24, "createTime": "2020-09-28T11:05:48", "updateTime": "2020-11-25T15:44:54" }, { "id": 17, "code": "10010500", "name": "非政府组织领域", "parentCode": "10010000", "level": 3, "sort": 5, "createTime": "2020-09-28T11:05:47", "updateTime": "2020-11-25T15:44:55" }, { "id": 63, "code": "10010101", "name": "境外敌对势力", "parentCode": "10010100", "level": 4, "sort": 1, "createTime": "2020-09-28T11:54:31", "updateTime": "2020-11-25T15:48:29" }, { "id": 64, "code": "10010102", "name": "左派", "parentCode": "10010100", "level": 4, "sort": 2, "createTime": "2020-09-28T11:54:32", "updateTime": "2020-11-25T15:48:29" }, { "id": 22, "code": "20010300", "name": "传销类", "parentCode": "20010000", "level": 3, "sort": 22, "createTime": "2020-09-28T11:05:48", "updateTime": "2020-11-25T15:44:54" }, { "id": 15, "code": "10010300", "name": "网络意识形态", "parentCode": "10010000", "level": 3, "sort": 3, "createTime": "2020-09-28T11:05:46", "updateTime": "2020-11-25T15:44:54" }, { "id": 20, "code": "20010100", "name": "P2P", "parentCode": "20010000", "level": 3, "sort": 20, "createTime": "2020-09-28T11:05:48", "updateTime": "2020-11-25T15:44:54" }, { "id": 13, "code": "10010100", "name": "反颠覆领域", "parentCode": "10010000", "level": 3, "sort": 1, "createTime": "2020-09-28T11:05:46", "updateTime": "2020-11-25T15:44:55" }, { "id": 32, "code": "20020700", "name": "烈士遗属", "parentCode": "20020000", "level": 3, "sort": 14, "createTime": "2020-09-28T11:05:50", "updateTime": "2020-11-25T15:44:54" }, { "id": 26, "code": "20020100", "name": "企业军转干部", "parentCode": "20020000", "level": 3, "sort": 10, "createTime": "2020-09-28T11:05:49", "updateTime": "2020-11-25T15:44:54" }, { "id": 28, "code": "20020300", "name": "农村义务兵", "parentCode": "20020000", "level": 3, "sort": 12, "createTime": "2020-09-28T11:05:49", "updateTime": "2020-11-25T15:44:54" }, { "id": 71, "code": "20020900", "name": "自主择业军转干部", "parentCode": "20020000", "level": 3, "sort": 15, "createTime": "2020-09-28T11:05:50", "updateTime": "2020-11-25T15:44:54" }, { "id": 81, "code": "10030400", "name": "精神传销", "parentCode": "10030000", "level": 3, "sort": 4, "createTime": "2020-12-15T16:13:05", "updateTime": null }, { "id": 79, "code": "10030200", "name": "冒用宗教名义(全能神)", "parentCode": "10030000", "level": 3, "sort": 2, "createTime": "2020-12-15T16:13:05", "updateTime": null }, { "id": 3, "code": "20000000", "name": "涉稳", "parentCode": "-1", "level": 1, "sort": 2, "createTime": "2020-09-17T18:10:38", "updateTime": "2020-11-25T15:41:01" }, { "id": 72, "code": "20021000", "name": "伤病残退役人员", "parentCode": "20020000", "level": 3, "sort": 16, "createTime": "2020-09-28T11:05:50", "updateTime": "2020-11-25T15:44:54" }], "code": "0", "message": null, "requestId": "d8d21242-d7df-4e5f-90d4-ab83e94bf869", "version": null, "success": true }
// console.log(obj)
function format(data) {
let result = []
data.forEach((child) => {
let ind = result.findIndex((item) => { return item.key === child.parentCode })
if (ind === -1) {
result.push({ key: child.parentCode, value: [] })
}
result.at(ind).value.push(child)
})
let fn = function (data) {
data.forEach((child) => {
let dataObj = result.find((item) => { return item.key === child.code })
if (dataObj) {
child.child = dataObj.value || []
child.child.length > 0 && fn(child.child)
}
})
}
fn(result.find((item) => { return item.key == '-1' }).value)
return result.find((item) => { return item.key == '-1' }).value
}
console.log(format(obj.data))
console