Skip to content

get

Reads a nested value from an object using a dot-notation path. Returns defaultValue (or undefined) when the path does not resolve. Never throws.

import { get } from "1o1-utils";
import { get } from "1o1-utils/get";
function get<T>({ obj, path, defaultValue }: GetParams<T>): unknown
NameTypeRequiredDescription
objTYesThe source object
pathstringYesDot-notation path (e.g. "address.city")
defaultValueunknownNoReturned when the path does not resolve

unknown — The value at path, or defaultValue when the path is unreachable.

get({ obj: { address: { city: "São Paulo" } }, path: "address.city" });
// => "São Paulo"
get({ obj: { a: 1 }, path: "a.b", defaultValue: "BR" });
// => "BR"
// Arrays via numeric segments
get({ obj: { items: [{ name: "a" }, { name: "b" }] }, path: "items.1.name" });
// => "b"
  • Returns defaultValue when the path is missing, obj is null/primitive, or path is empty.
  • Never throws — get is the “safe access” escape hatch.
  • defaultValue: null is preserved and not treated as undefined.
  • Values set to undefined on the target return defaultValue; values set to null are returned as-is.

read nested, dot notation, deep access, safe access, property path, lodash get

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use get to safely read a nested field from an API response with a default fallback