🎯 Gets the value at a specific path of an object.

Info

Supports both string paths (e.g., 'a.b[0].c') and array paths (e.g., ['a', 'b', 0, 'c']).

Syntax

TypeScript
import { get } from '@opentf/std';

get(
  obj: object,
  path: string | unknown[],
  defVal?: unknown
): unknown

Parameters

  • obj: The object to query.

  • path: The path of the property to get.

  • defVal: The value returned when the path doesn't exist. Does not replace an explicitly undefined value at the resolved path.

Returns

The resolved value or the default value.

Examples

TypeScript
get({ a: 1 }, 'a') //=> 1

get({ user: { email: 'user@example.com' } }, 'user.email') //=> 'user@example.com'

get({ a: [{ b: { c: 99 } }] }, 'a[0].b.c') //=> 99

get({ a: 1 }, 'b', 'default') //=> 'default'

get({ a: undefined }, 'a', 'default') //=> undefined

get({ a: null }, 'a') //=> null (null is a valid value)

get({ fruits: ['Apple', 'Mango'] }, ['fruits', 1]) //=> 'Mango'
Last updated on
Edit this page