编辑代码

//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
const obj = {
    selector: {
        to: {
            name: 'fe coder'
        }
    },
    target: [1,2,{name: 'llz'}]
}

function getByProp(obj, param) {
    const pathArray = param.split(/[\.\[\]]/).filter(p=>p!=='')
    return pathArray.reduce((acc, path)=>{
        return acc[path]
    }, obj)
}
function get(obj, ...params){
    const result = []
    for(let i=0;i<=params.length-1;i++){
        const param = params[i]
        result.push(getByProp(obj, param))
    }
    return result
}

/**
 * expect: ['fe coder', 1, 'llz']
 */
const result = get(obj, 'selector.to.name', 'target[0]', 'target[2].name')
console.log(result)