js 多级树形数据包装 更改格式

可用于将其转换为,想要的树形数据

品,你品,你在品,你细品

let menuData = [
    {
        id: 1,
        parentId:0,
        icon: 'dashboard',
        level: 1,
        name: '仪表盘'
    },
    {
        id: 2,
        parentId:0,
        icon: 'shopping',
        level: 1,
        name: '商品管理'
    },
    {
        id: 3,
        parentId:0,
        icon: 'snippets',
        level: 1,
        name: '订单管理'
    },
    {
        id: 5,
        parentId:0,
        icon: 'setting',
        level: 0,
        name: '设置',
        children: [
            {
                id: 51,
                parentId:5,
                icon: 'menu',
                level: 1,
                name: '菜单管理'
            },
            {
                id: 52,
                parentId:5,
                icon: 'deployment-unit',
                level: 1,
                name: '角色管理'
            },
            {
                id: 53,
                parentId:5,
                icon: 'usergroup-add',
                level: 0,
                name: '用户管理',
                children:[
                    {
                        id: 531,
                        parentId:53,
                        icon: 'team',
                        level: 1,
                        name: '平台用户'  
                    },
                    {
                        id: 532,
                        parentId:53,
                        icon: 'solution',
                        level: 1,
                        name: '系统用户'  
                    }
                ]
            }
        ]
    }
];

function cb (item){
    let newObj = new Object
    newObj.key = item.id
    newObj.value = item.id
    newObj.title = item.name
    if(!!item.children){
        newObj.children = conversionData(cb,item.children)
    }
    return newObj
}

function conversionData( cb , data){
    return data.map(item =>cb(item))
}

conversionData(cb,menuData)

你可能感兴趣的:(React)