function reTree(array, id = 'value', fid = 'feather', clb = 'children'){
let idMap = {};
let tree = [];
array.forEach(item => idMap[item[id]] = item); // 构建以id为键的对象
for(const item of array){
let feather = idMap[item[fid]]; // item 的父元素
delete item[fid];
if(feather){ // 有 item 的父元素
if(clb in feather){ // 是否有子元素的字段数组
feather[clb].push(item)
}else{
feather[clb] = [];
feather[clb].push(item);
}
}else{ // 没有父元素,说明是根节点
tree.push(item)
}
}
return tree;
}
let arr = [{ "value": "zhinan", "label": "指南", "feather": null }, { "value": "shejiyuanze", "label": "设计原则", "feather": "zhinan" }, { "value": "yizhi", "label": "一致", "feather": "shejiyuanze" }, { "value": "fankui", "label": "反馈", "feather": "shejiyuanze" }, { "value": "xiaolv", "label": "效率", "feather": "shejiyuanze" }, { "value": "kekong", "label": "可控", "feather": "shejiyuanze" }, { "value": "daohang", "label": "导航", "feather": "zhinan" }, { "value": "cexiangdaohang", "label": "侧向导航", "feather": "daohang" }, { "value": "dingbudaohang", "label": "顶部导航", "feather": "daohang" }, { "value": "zujian", "label": "组件", "feather": null }, { "value": "basic", "label": "Basic", "feather": "zujian" }, { "value": "layout", "label": "Layout 布局", "feather": "basic" }, { "value": "color", "label": "Color 色彩", "feather": "basic" }, { "value": "typography", "label": "Typography 字体", "feather": "basic" }, { "value": "icon", "label": "Icon 图标", "feather": "basic" }, { "value": "button", "label": "Button 按钮", "feather": "basic" }, { "value": "form", "label": "Form", "feather": "zujian" }, { "value": "radio", "label": "Radio 单选框", "feather": "form" }, { "value": "checkbox", "label": "Checkbox 多选框", "feather": "form" }, { "value": "input", "label": "Input 输入框", "feather": "form" }, { "value": "input-number", "label": "InputNumber 计数器", "feather": "form" }, { "value": "select", "label": "Select 选择器", "feather": "form" }, { "value": "cascader", "label": "Cascader 级联选择器", "feather": "form" }, { "value": "switch", "label": "Switch 开关", "feather": "form" }, { "value": "slider", "label": "Slider 滑块", "feather": "form" }, { "value": "time-picker", "label": "TimePicker 时间选择器", "feather": "form" }, { "value": "date-picker", "label": "DatePicker 日期选择器", "feather": "form" }, { "value": "datetime-picker", "label": "DateTimePicker 日期时间选择器", "feather": "form" }, { "value": "upload", "label": "Upload 上传", "feather": "form" }, { "value": "rate", "label": "Rate 评分", "feather": "form" }, { "value": "form", "label": "Form 表单", "feather": "form" }, { "value": "data", "label": "Data", "feather": "zujian" }, { "value": "table", "label": "Table 表格", "feather": "data" }, { "value": "tag", "label": "Tag 标签", "feather": "data" }, { "value": "progress", "label": "Progress 进度条", "feather": "data" }, { "value": "tree", "label": "Tree 树形控件", "feather": "data" }, { "value": "pagination", "label": "Pagination 分页", "feather": "data" }, { "value": "badge", "label": "Badge 标记", "feather": "data" }, { "value": "notice", "label": "Notice", "feather": "zujian" }, { "value": "alert", "label": "Alert 警告", "feather": "notice" }, { "value": "loading", "label": "Loading 加载", "feather": "notice" }, { "value": "message", "label": "Message 消息提示", "feather": "notice" }, { "value": "message-box", "label": "MessageBox 弹框", "feather": "notice" }, { "value": "notification", "label": "Notification 通知", "feather": "notice" }, { "value": "navigation", "label": "Navigation", "feather": "zujian" }, { "value": "menu", "label": "NavMenu 导航菜单", "feather": "navigation" }, { "value": "tabs", "label": "Tabs 标签页", "feather": "navigation" }, { "value": "breadcrumb", "label": "Breadcrumb 面包屑", "feather": "navigation" }, { "value": "dropdown", "label": "Dropdown 下拉菜单", "feather": "navigation" }, { "value": "steps", "label": "Steps 步骤条", "feather": "navigation" }, { "value": "others", "label": "Others", "feather": "zujian" }, { "value": "dialog", "label": "Dialog 对话框", "feather": "others" }, { "value": "tooltip", "label": "Tooltip 文字提示", "feather": "others" }, { "value": "popover", "label": "Popover 弹出框", "feather": "others" }, { "value": "card", "label": "Card 卡片", "feather": "others" }, { "value": "carousel", "label": "Carousel 走马灯", "feather": "others" }, { "value": "collapse", "label": "Collapse 折叠面板", "feather": "others" }, { "value": "ziyuan", "label": "资源", "feather": null }, { "value": "axure", "label": "Axure Components", "feather": "ziyuan" }, { "value": "sketch", "label": "Sketch Templates", "feather": "ziyuan" }, { "value": "jiaohu", "label": "组件交互文档", "feather": "ziyuan" }];
console.log(reTree(arr, 'value','feather'))
console