js怎样通过递归的方式改变树状数据的key值?

张巨侠
  • 286
有一段树状结构的数据(后端传来的children不确定多少级),需要把key(value、label、children)换成(id, text, arr)
const aa =
    [{
        value: 'zhinan',
        label: '指南',
        children: [{
            value: 'shejiyuanze',
            label: '设计原则',
            children: [{
                value: 'yizhi',
                label: '一致',
            }, {
                value: 'fankui',
                label: '反馈'
            }, {
                value: 'xiaolv',
                label: '效率'
            }, {
                value: 'kekong',
                label: '可控'
            }]
        }, {
            value: 'daohang',
            label: '导航',
            children: [{
                value: 'cexiangdaohang',
                label: '侧向导航'
            }, {
                value: 'dingbudaohang',
                label: '顶部导航'
            }]
        }]
    }, {
        value: 'zujian',
        label: '组件',
        children: [{
            value: 'basic',
            label: 'Basic',
            children: [{
                value: 'layout',
                label: 'Layout 布局'
            }, {
                value: 'color',
                label: 'Color 色彩'
            }, {
                value: 'typography',
                label: 'Typography 字体'
            }, {
                value: 'icon',
                label: 'Icon 图标'
            }, {
                value: 'button',
                label: 'Button 按钮'
            }]
        }, {
            value: 'form',
            label: 'Form',
            children: [{
                value: 'radio',
                label: 'Radio 单选框'
            }, {
                value: 'checkbox',
                label: 'Checkbox 多选框'
            }, {
                value: 'input',
                label: 'Input 输入框'
            }, {
                value: 'input-number',
                label: 'InputNumber 计数器'
            }, {
                value: 'select',
                label: 'Select 选择器'
            }, {
                value: 'cascader',
                label: 'Cascader 级联选择器'
            }, {
                value: 'switch',
                label: 'Switch 开关'
            }, {
                value: 'slider',
                label: 'Slider 滑块'
            }, {
                value: 'time-picker',
                label: 'TimePicker 时间选择器'
            }, {
                value: 'date-picker',
                label: 'DatePicker 日期选择器'
            }, {
                value: 'datetime-picker',
                label: 'DateTimePicker 日期时间选择器'
            }, {
                value: 'upload',
                label: 'Upload 上传'
            }, {
                value: 'rate',
                label: 'Rate 评分'
            }, {
                value: 'form',
                label: 'Form 表单'
            }]
        }, {
            value: 'data',
            label: 'Data',
            children: [{
                value: 'table',
                label: 'Table 表格'
            }, {
                value: 'tag',
                label: 'Tag 标签'
            }, {
                value: 'progress',
                label: 'Progress 进度条'
            }, {
                value: 'tree',
                label: 'Tree 树形控件'
            }, {
                value: 'pagination',
                label: 'Pagination 分页'
            }, {
                value: 'badge',
                label: 'Badge 标记'
            }]
        }, {
            value: 'notice',
            label: 'Notice',
            children: [{
                value: 'alert',
                label: 'Alert 警告'
            }, {
                value: 'loading',
                label: 'Loading 加载'
            }, {
                value: 'message',
                label: 'Message 消息提示'
            }, {
                value: 'message-box',
                label: 'MessageBox 弹框'
            }, {
                value: 'notification',
                label: 'Notification 通知'
            }]
        }, {
            value: 'navigation',
            label: 'Navigation',
            children: [{
                value: 'menu',
                label: 'NavMenu 导航菜单'
            }, {
                value: 'tabs',
                label: 'Tabs 标签页'
            }, {
                value: 'breadcrumb',
                label: 'Breadcrumb 面包屑'
            }, {
                value: 'dropdown',
                label: 'Dropdown 下拉菜单'
            }, {
                value: 'steps',
                label: 'Steps 步骤条'
            }]
        }, {
            value: 'others',
            label: 'Others',
            children: [{
                value: 'dialog',
                label: 'Dialog 对话框'
            }, {
                value: 'tooltip',
                label: 'Tooltip 文字提示'
            }, {
                value: 'popover',
                label: 'Popover 弹出框'
            }, {
                value: 'card',
                label: 'Card 卡片'
            }, {
                value: 'carousel',
                label: 'Carousel 走马灯'
            }, {
                value: 'collapse',
                label: 'Collapse 折叠面板'
            }]
        }]
    }, {
        value: 'ziyuan',
        label: '资源',
        children: [{
            value: 'axure',
            label: 'Axure Components'
        }, {
            value: 'sketch',
            label: 'Sketch Templates'
        }, {
            value: 'jiaohu',
            label: '组件交互文档'
        }]
    }]

自己写了一段怎么也不成功,不知道如何去写?

interface Cascader {
    id: string | number,
    text: string,
    arr?: []
}

// 转换数据
const getFaultOptions = (data: any) => {
    let newArr:any = []
    function handleData(arr:[]) {
        arr.map((item: any) => {
            let cascad: Cascader = { id: '', text: '' }
            cascad['id'] = item.label
            cascad['text'] = item.value
            if (item.children && item.children.length) {
                cascad.arr = []
                handleData(item.children)
            }
            newArr.push(cascad)
        })
    }
    handleData(data)
    return newArr
}
console.log(getFaultOptions(aa))
回复
阅读 404
4 个回答
✓ 已被采纳
interface Data {
  value: string;
 label: string;
 children?: Array<Data>;
}
interface Cascader {
  id: string | number;
 text: string;
 arr?: Array<Cascader>;
}
function test(tree: Array<Data>): Array<Cascader> {
  const outArray: Array<Cascader> = [];
 for (const data of tree) {
    const out: Cascader = {text: data.label, id: data.value};
 if (data.children && data.children.length) {
      out.arr = test(data.children);
 }
    outArray.push(out);
 }
  return outArray;
}

我用JS写了一段,应该能解决你这个问题。

function a2b(ls){
    return ls.map(obj=>{
        let result = {id:'',text:''}
        if(obj.children && obj.children.length > 0){
            result.id = obj.value
            result.text = obj.label
            result.arr = a2b(obj.children)
            return result
        } else {
            result.id = obj.value
            result.text = obj.label
            return result
        }
    })
}
interface CascaderData {
    value: string | number,
    label: string,
    children?: CascaderData[]
}
interface Cascader {
    id: string | number,
    text: string,
    arr?: Cascader[]
}

const convert = (data?: CascaderData[]): Cascader[] => data?.map(child => ({
  id: child.value,
  text: child.label,
  arr: convert(child.children)
}))

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

console.log(convert(data))
你知道吗?

宣传栏