getValueByPath

2020/12/28

根据属性字符串从对象中取值

getValueByPath(obj, prop)

# 参数

  1. obj(Object) :对象数据
  2. prop(String):属性路径字符串

# 返回值

(any|undefined):查找的结果

# 例子

import { getValueByPath } from '@liz-q/utils'

const obj = {
    a: {
        b: {
            c: 1
        }
    },
    d: 2
}
getValueByPath(obj, 'd') // 2
getValueByPath(obj, 'a.b.c') // 1
getValueByPath(obj, 'a.b')
// {
//    c: 1
// }
Last Updated: 2023/11/13 下午5:46:22