Skip to main content
@spa-tools/utilities

getNestedValue()

As much as we'd like to always access data statically with properties known at build time, the reality is that sooner or later you're going to need to access nested data via a dot-notated string.

Should this requirement ever rear its ugly head, you can use the getNestedValue<T>() function to retrieve nested data values via a string in a type-safe manner.

The generic T type is used to define the type of the property that is returned.

Usage

import { getNestedValue } from '@spa-tools/utilities';

const person = {
address: {
city: 'New York',
country: 'USA',
},
childrenAges: [8, 10],
id: 1,
name: 'John',
};

const city = getNestedValue(person, 'address.city', 'unknown city');
console.log(city);
// --> 'New York'

const zipCode = getNestedValue(person, 'address.zipCode', 'n/a');
console.log(zipCode);
// --> 'n/a'

Arguments

NameTypeDescription
dataunknownThe object holding nested property to access
dataDotPathstringThe string dot-notation path of property location
defaultValueTThe value to return if the property is not found

Returns

Returns either the found property value or default value as T.