首页 > 其他分享 >判断多层嵌套的对象里面的属性是否存在的问题

判断多层嵌套的对象里面的属性是否存在的问题

时间:2023-02-20 17:26:29浏览次数:33  
标签:obj value 嵌套 let 多层 path const pathStr 属性

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

相关文章