lodash.get 是做什么的
有时我们有这样的需求——需要获取某个结构中比较深层次的一个属性,比如这样:
const obj = {
a:{
b:{
c:{
name:'zhou'
}
}
}
}
想要拿到里面的name
,也就是说需要一层层的去查找。但有时还不能确定这条路径是否正确,这还需要避免发生TypeError
错误。像这样:
const obj = {
a:{
b:{
c:{
name:'zhou'
}
}
}
}
a && a.b && a.b.c && a.b.c.name
而lodash.get
就是解决这个问题的,用这个方法就可以简化为这样:
_.get(obj, 'a.b.c.name')
如何实现 lodash.get
首先看看文档:
_.get(object, path, [defaultValue])
根据
object
对象的path
路径获取值。 如果解析value
是undefined
会以defaultValue
取代。
关于其参数:
- 比较特殊的一点就是
path
可以是Array|string
。 - 以及传入的 object 可以是嵌套对象,也可以是嵌套的数组,也就是说
path
中也可以是数组的路径
数组的路径表示是用 []
的,所以要先将其全部转换为.
运算符,之后再组成数组
//a[0].b -> a.0.b -> ['a', '0', 'b']
const tpath = path.replace(/\[(\d+)\]/g,'.$1').split('.')
然后就是一层层往下走就完事了:
function get(object, path, defaultValue = undefined){
const tpath = path.replace(/\[(\d+)\]/g,'.$1').split('.')
let res = object
for(const t of tpath){
res = res[p]
if(res === undefined) return defaultValue
}
return res
}
上面的代码乍一看好像没有问题,实际上:res
一开始是有可能为 undefined
的,这样就会报出TypeError: Cannot read properties of undefined
的错误。
我们可以这样改进一下那行代码
function get(object, path, defaultValue = undefined){
const tpath = path.replace(/\[(\d+)\]/g,'.$1').split('.')
let res = object
for(const t of tpath){
res = Object(res)[p] //*
if(res === undefined) return defaultValue
}
return res
}
这样如果res
为空,那么也会先变为一个空对象{}
而已,访问不不存在的键也就是返回符合预期的 undefined
可选链
ES2020
新增 可选链条 ?.
非常方便,像上面的需求可以直接:
const obj = {
a:{
b:{
c:{
name:'zhou'
}
}
}
}
obj?.a?.b?.c?.name