BUPKIS
    Preparing search index...

    Function get

    • Retrieves the value at a given keypath from an object using dot or bracket notation.

      Supports keypaths like:

      • 'foo.bar' - dot notation
      • 'foo[1].spam' - bracket notation with array indices
      • 'foo["bar-baz"].quux' - bracket notation with quoted strings
      • "foo['bar-baz'].quux" - bracket notation with single quotes

      This function cannot retrieve values associated with symbol keys.

      Type Parameters

      • T = unknown

      Parameters

      • obj: unknown

        The object to retrieve the value from

      • keypath: string

        The keypath using dot or bracket notation

      • OptionaldefaultValue: T

        Optional default value to return if the keypath is not found

      Returns T | undefined

      The value at the keypath, or defaultValue if not found

      const obj = {
      foo: {
      bar: 'hello',
      'bar-baz': { quux: 'world' },
      },
      arr: [{ spam: 'eggs' }],
      };

      get(obj, 'foo.bar');
      get(obj, 'arr[0].spam');
      get(obj, 'foo["bar-baz"].quux');
      get(obj, 'foo.nonexistent');
      get(obj, 'foo.nonexistent', 'default');