element级联选择器,递归求出所有可选择值的组合

198 阅读1分钟
let options = [{    value: 'zhinan',    label: '指南',    children: [{        value: 'shejiyuanze',        label: '设计原则',        children: [{            value: 'yizhi',            label: '一致'        },{            value: 'yizhi2',            label: '一致2'        }        ]
    },
        {
            value: 'shejiyuanze2',
            label: '设计原则2',
            children: [{
                value: 'yizhi3',
                label: '一致3'
            },
                {
                    value: 'yizhi10',
                    label: '一致4'
                }]
        }
    ]
}
]
let pathList = []
function getPath(options,path,pathList){
    for(let option of options){
        let currentPath = path.concat([option.value])
        if(option.children){
            getPath(option.children,currentPath,pathList)
        } else{
            pathList.push(currentPath)
        }

    }
}
getPath(options,[],pathList)
console.log(pathList)
> ````引用自佳鹏大佬