const obj = {
name: 'lihua',
other: {
love: {
food: 'apple'
},
face: 'beauty'
}
}
getValueByPath = (obj,path)=> {
let value = obj
let pathStr = path.split('.')
for (const str of pathStr) {
try {
value = value[str]
} catch (error) {
value = undefined
}
}
return value
}
标签:obj,value,嵌套,let,多层,path,const,pathStr,属性
From: https://www.cnblogs.com/luckyitape/p/17138187.html