Skip to content

Function: get() ​

ts
function get<T>(
   object, 
   path, 
   defaultValue?): unknown;

Safely gets a nested value from an object by path. 通过路径安全地获取对象中的嵌套值。

Type Parameters ​

Type ParameterDefault typeDescription
TundefinedThe fallback value type / 兜底值类型

Parameters ​

ParameterTypeDescription
objectunknownThe source object / 源对象
pathstring | readonly PropertyKey[]The access path / 访问路径
defaultValue?TThe fallback value returned when the path does not exist / 路径不存在时返回的兜底值

Returns ​

unknown

The found value or the fallback value / 找到的值或兜底值

Example ​

ts
get({ a: { b: 2 } }, 'a.b')
// => 2

Since ​

1.2.0

Released under the MIT License.