Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Retrieve Nested Property Values in JavaScript / Typescript

DZone's Guide to

How to Retrieve Nested Property Values in JavaScript / Typescript

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

Use the following function to retrieve the value of a property nested within another property in JavaScript...

export function retriveValueFromNestedProperty(objectToIterate: any, propertyName: string): any {

    var array: Array<string> = propertyName.split(".");

    var currentValue: any = objectToIterate[array[0]];

    for (var i = 1; i < array.length; i++) {

        currentValue = currentValue[array[i]];
    }

    return currentValue;
}


Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}