Object
has

🎯 Checks if a specific path exists in an object.

This function uses Object.hasOwn() internally, so it correctly detects properties even if their value is undefined or null.

Syntax

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

Parameters

  • obj: The object to inspect.
  • path: The path to check.

Returns

true if the path exists, false otherwise.

Examples

has({ a: 1 }, 'a') //=> true
 
has({ a: undefined }, 'a') //=> true
 
has({ a: null }, 'a') //=> true
 
has({ a: { b: [1, 2, 3, { c: 5 }] } }, 'a.b[3].c') //=> true
 
has({ a: { b: [1, 2, 3, { c: 5 }] } }, 'a.b[5].c') //=> false
 
has({ a: { 'b.c': 1 } }, ['a', 'b.c']) //=> true

Related

Try