const arr = [ { id: 1, parentId: 0, name: "员工激励" }, { id: 2, parentId: 1, name: "个人表彰" }, { id: 3, parentId: 1, name: "项目表彰" }, { id: 4, parentId: 2, name: "四五星优秀员工表彰产品" }, { id: 5, parentId: 2, name: "员工即使激励表彰产品" }, { id: 6, parentId: 3, name: "项目表彰产品" }, { id: 7, parentId: 0, name: "员工关怀" }, { id: 8, parentId: 7, name: "员工生日会" }, { id: 9, parentId: 7, name: "入职周年庆" }, { id: 10, parentId: 7, name: "节日福利" }, { id: 11, parentId: 7, name: "节庆晚会" }, { id: 12, parentId: 7, name: "协助赛事" }, { id: 13, parentId: 10, name: "教师节活动产品" }, { id: 14, parentId: 10, name: "感恩节活动产品" }, { id: 15, parentId: 10, name: "圣诞节活动产品" }, { id: 16, parentId: 10, name: "其他节日类活动产品" }, { id: 17, parentId: 11, name: "年会活动产品" }, { id: 18, parentId: 0, name: "员工发展" }, { id: 19, parentId: 18, name: "培训类" }, { id: 20, parentId: 18, name: "会议会务" }, { id: 21, parentId: 19, name: "常规培训类" }, { id: 22, parentId: 19, name: "项目型培训" }, { id: 23, parentId: 21, name: "授课式培训活动产品" }, { id: 24, parentId: 21, name: "分享交流会培训活动产品" }, { id: 25, parentId: 22, name: "培训启动仪式活动产品" }, { id: 26, parentId: 22, name: "培训结项仪式活动产品" }, { id: 27, parentId: 22, name: "训练营活动产品" }, { id: 28, parentId: 20, name: "员工大会" }, { id: 29, parentId: 20, name: "交流会" }, { id: 30, parentId: 29, name: "座谈会" }, { id: 31, parentId: 29, name: "主题沙龙" }, { id: 32, parentId: 0, name: "团队建设" }, { id: 33, parentId: 32, name: "员工团建" }, { id: 34, parentId: 32, name: "团队拓展" }, { id: 35, parentId: 33, name: "新人团建活动产品" }, { id: 36, parentId: 33, name: "员工年度团建活动产品" }, { id: 37, parentId: 34, name: "室内拓展活动产品" }, { id: 38, parentId: 34, name: "户外拓展活动产品" }, ]; function getRouteTree(arr) { let route = []; arr.forEach((item) => { // 获取parentId = 当前id const children = arr.filter((child) => child.parentId == item.id); if (children.length) item.children = children; }); // 取出最顶层 parentId == 0 route = arr.filter((item) => item.parentId == 0); return JSON.stringify({ route }); }