German
German

Reputation: 10402

Value check in nested javascript object (might not exist)

Is there an elegant and concise way to avoid doing something like this to check a value deep within an object member hierarchy in javascript?

handlerInput.supportsDisplay = function() {
   return  this.requestEnvelope.context && 
           this.requestEnvelope.context.System && 
           this.requestEnvelope.context.System.device && 
           this.requestEnvelope.context.System.device.supportedInterfaces && 
           this.requestEnvelope.context.System.device.supportedInterfaces.Display;
}

Upvotes: 3

Views: 65

Answers (3)

Steven Spungin
Steven Spungin

Reputation: 29109

I wrote an NPM module that allows you to query by a string path.

https://www.npmjs.com/package/js-path-resolver
http://www.twelvetone.tv/docs/developer-tools/npm-modules/js-path-resolver

Here's a codepen https://codepen.io/Flamenco/pen/xaVKjR/?editors=1111

For your use case:

import resolver from 'js-path-resolver'
const info = resolver(handlerInput, 'requestEnvelope.context.System.device.supportedInterface.Display', {onError:'continue'})
const hasDisplay = info.exists
const display = info.get()

In a nutshell, you split the string, then try to resolve the path, one string at a time. There are all sorts of issues implementing this, such as escaping dots, handling array indexes, etc, so using a library makes life much easier. In this library, you can also set, and delete the resolved path.

Upvotes: 0

Mikhail Burshteyn
Mikhail Burshteyn

Reputation: 5002

There is currently no concise way to do it in plain JavaScript, unless you use a helper function or a third-party library.

There is a proposal (at Stage 1 as of August 2018) to add the ?. operator to JavaScript which does exactly what you want.

With that proposal accepted, you would be able to use ?. instead of . anywhere where a property might be missing, so your code would become:

// careful: this syntax is not available yet
var hasDisplay = handlerInput.requestEnvelope.context?.System?.device?.supportedInterfaces?.Display

Upvotes: 1

seebiscuit
seebiscuit

Reputation: 5053

This is cheating, but you can use the _.get function from Lodash

_.get(validations, "path.to.nested.prop");

From the docs

Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.

Upvotes: 0

Related Questions