getIn<T
>(p
, o
): T
| null
主动防御 对于我们操作的数据,尤其是由 API 接口返回的,时常会有一个很复杂的深层嵌套的数据结构。为了代码的健壮性,很多时候需要对每一层访问都作空值判断,就像这样: props.user && props.user.posts && props.user.posts[0] && props.user.posts[0].comments && props.user.posts[0].comments[0] 代码看起来相当不美观,因此提供了一个非常简洁明了的原生的方式。
Name |
---|
T |
Name | Type | Description |
---|---|---|
p |
(string | number )[] |
属性列表 |
o |
unknown |
对象 |
T
| null
如果正常访问到,则返回对应的值,否则返回 null。
Example
Example