Skip to content

Function: getObjectPropertyPaths() ​

getObjectPropertyPaths(target, basePath?): string[]

List the dot-separated paths of the leaf values of an object (b.c for {b: {c: 2}}), in the order of its keys, depth first. Only the enumerable own string keys are walked; the arrays give their indexes (list.0), and every object is walked, a Date or a Map included, so it gives no path of its own. A nested empty object or array gives no path. A null value is a leaf, and so is a circular reference (an object containing one of the objects it is in), which is not walked again. An object referenced twice, not circularly, is walked at each of its paths.

Parameters ​

target ​

Record<string, any>

The object to walk.

basePath? ​

string

The path prefixed to every path, joined with a dot (root gives root.a); none by default.

Returns ​

string[]

The paths of the values which are not objects (null and undefined values included) and of the circular references; an empty object gives [].

Throws ​

When target is null or undefined.

Example ​

typescript
import {getObjectPropertyPaths} from 'lakutata/utils'

console.log(getObjectPropertyPaths({a: 1, b: {c: 2, d: {e: 3}}, list: [4, 5], none: null}))
//['a', 'b.c', 'b.d.e', 'list.0', 'list.1', 'none']

Released under the MIT License.