Node.js deep object property checking in if condition

How many times you wrote if condition in your node.js code? What about checking an objects property in if condition? How many times you needed to check an optional property of an object in an if condition? And what about that the object is a complex one, like an array or nested or mix up?

Let’s simplify the questions with an example problem.

Let’s say we have a user object and we need to print the ‘name(a property) of that object.

Let’s write a simple function to do this

function printName(user){  console.log(user.name);}

This looks fine, but it will give an error if the input object is null or undefined. So to safely run the code let’s add an if condition in the function.

function printName(user){   if(user && user.name){       console.log(user.name);    }}

Perfect! We have saved our code from being crashed.

But what about working with a nested object where in any nested depth there can be optional property. How will we use if condition in a simplified way?

Let’s look at another example problem.

This time let’s say we will be given a user object which may have a name and a list of contact (array).

We need to print the secondary contact number of that user.

If no secondary contact exist then print ‘No secondary contact’.

The object may look like this.

const user = {
name: ’John’,
contact: [
{
countryCode: “880”,
phone: “123456789”
},
{
countryCode: “880”,
phone: “123456789”
}
]
};

So similar as before let’s write a function to do so.

Also write the code with safe checking

function printSecondary(user){       if(user 
&& user.contact
&& user.contact[1]
&& user.contact[1].phone){
console.log(user.contact[1].phone);} else{ console.log(“No secondary contact”) }}

Huh!! look the if block!!

user && user…. && …..

We had to write ‘user’ 4 times to safely check the 4th condition which is the actual value we want to know.

And also look at the length of the if block. It may not complete in a single line! Huh! just a simple if condition is multi-line!!!

Conditional hell!!! (like the callback hell!!)

So to solve the problem I’ve written a simple package (has-deep-prop) using a simple loop. With no dependency.

My package function receives an object which can be anything.

Like a normal JavaScript object or an array or mix of them. Even null, undefined are also acceptable as the object value. But in that case the return value will be undefined.

It saves you one more time of checking whether the input object is undefined or null before passing to the function parameter. You can safely pass anything you want.

The second parameter of has-deep-prop is a string which should represent the deep props separated by dot (.)

For example, in the given problem the string format of user.contact[1].phone should be ‘user.contact.1.phone’.

Look here, to access a value in array we have simply added the array index after dot.

The package function will return the deep property value we want to know. If not found then undefined will be returned.

So our previous solution can be re-written as

const hasDeep = require(‘has-deep-prop’);function printSecondary(user){    const secondaryContact = hasDeep(user, ‘contact.1.phone’);    if(secondaryContact){        console.log(secondaryContact);
}
else { console.log(“No secondary contact”);
}
}

Simple!! Saved lot’s of code and made the condition checking easier and readable.

Saved writing the same variable again and again…

Happy coding!!!

Associate software engineer @BrainStation-23 since 2020

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store