Skip to content

Function: getObjectNestingDepth() ​

getObjectNestingDepth(target): number

Measure how deeply an object nests: 0 for an object without enumerable own string keys, 1 for an object whose values are not objects, plus one for each level of nested objects or arrays (the deepest branch counts). A nested empty object or array adds nothing, the arrays count by their indexes, and a string counts as an object of its characters (1). A null value is a leaf, and a circular reference (an object containing one of the objects it is in) is not walked again: it adds nothing.

Parameters ​

target ​

Object

The object to measure.

Returns ​

number

The nesting depth, 0 or more.

Throws ​

When target is null or undefined.

Example ​

typescript
import {getObjectNestingDepth} from 'lakutata/utils'

console.log(getObjectNestingDepth({}), getObjectNestingDepth({a: 1}), getObjectNestingDepth({a: {b: {c: 1}}}))
//0 1 3
console.log(getObjectNestingDepth({a: null, b: {c: null}}))
//2

Released under the MIT License.