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
Section titled “Import”import { get } from "1o1-utils";import { get } from "1o1-utils/get";Signature
Section titled “Signature”function get<T>({ obj, path, defaultValue }: GetParams<T>): unknownParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
obj | T | Yes | The source object |
path | string | Yes | Dot-notation path (e.g. "address.city") |
defaultValue | unknown | No | Returned when the path does not resolve |
Returns
Section titled “Returns”unknown — The value at path, or defaultValue when the path is unreachable.
Examples
Section titled “Examples”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 segmentsget({ obj: { items: [{ name: "a" }, { name: "b" }] }, path: "items.1.name" });// => "b"Edge Cases
Section titled “Edge Cases”- Returns
defaultValuewhen the path is missing,objisnull/primitive, orpathis empty. - Never throws —
getis the “safe access” escape hatch. defaultValue: nullis preserved and not treated as undefined.- Values set to
undefinedon the target returndefaultValue; values set tonullare returned as-is.
Also known as
Section titled “Also known as”read nested, dot notation, deep access, safe access, property path, lodash get
Prompt suggestion
Section titled “Prompt suggestion”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