Object
get

Gets the value of an object at the given path.

You can provide an optional default value to return if the given path does not exist.

Syntax

import { get } from '@opentf/std';
 
get(
  obj: object,
  path: string | unknown[],
  defVal?: unknown
): unknown

Examples

get({}, '') //=> undefined
 
get({}, '', undefined) //=> undefined
 
get({}, '', null) //=> null
 
get({}, 'a') //=> undefined
 
get({ a: null }, 'a') //=> null
 
get({ a: 1 }, 'a') //=> 1
 
get({ user: { email: 'user@example.com' } }, 'user.email'); // 'user@example.com'
 
get({ fruits: ['Apple', 'Mango', 'Orange'] }, 'fruits'); // ['Apple', 'Mango', 'Orange']
 
get({ a: [{ b: { c: 99 } }] }, 'a[0].b.c'); // 99

Try