Skip to content

get

The get function accesses the value of a nested property in an object using a path (string or array of strings). It allows you to set a default value if the path does not exist.

Syntax

typescript
get<T>(obj: T, path: string | string[], defaultValue?: any): any

Parameters

NameTypeDescription
objTSource object.
pathstring | string[]Path to the property (e.g., "a.b.c" or ["a", "b", "c"]).
defaultValueany (optional)Default value if the path does not exist.

Return

TypeDescription
anyReturns the value found at the specified path, or defaultValue if the path does not exist.

Examples

typescript
const obj = { a: { b: { c: 42 } } };
get(obj, 'a.b.c'); // 42
get(obj, ['a', 'b', 'c']); // 42
get(obj, 'a.b.x', 0); // 0

Notes

  • Supports paths with array notation (e.g., "a.b[0].c").
  • Useful for safely accessing nested properties.

References

Released under the MIT License.