获取传入getValue函数相应对象的key的value
// 请实现一个getValue函数,该函数可以从一个深层对象中获取到该对象上的值, 该函数接受两个参数, 一个是需要获取值的原始对象(originObj), 第二个参数是需要从深层对象中获取value使用的key的路径(keyPath)。
let obj = {
a: {
b: {
c: {
d: 1
}
}
}
}
function getValue(obj, path) {
let arr = path.split('.')
let val = obj
arr.forEach((item, index) => {
val = val[item]
})
return val
}
var res = getValue(obj, 'a.b.c.d')
console.log('res', res);
var res2 = getValue(obj, 'a.b')
console.log('res2', res2);