const obj = {
selector: {
to: {
toutiao: "FE Coder"
}
},
target: [
1,
2,
{
name: 'byted'
}
]
};
get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name')
['FE coder', 1, 'byted']
function get(obj,...path){
return path.map((item) => {
let temp = deep_copy(obj)
item.replace(/\[/g,'.')
.replace(/\]/g,'')
.split('.')
.map((key) => tmep && temp[key])
return temp
})
}
function deep_copy(obj){
if(typeof(obj) !== 'object' || obj == null){
return obj
}
let newObj = Array.isArray(obj) ? [] : {}
for(let key in obj){
newObj[key] = deep_copy(obj[key])
}
return newObj
}