JavaScript — Nested Objects

Vilva Athiban P B
1 min readJul 19, 2018

Generally, we get to deal with nested objects with higher depths as well. It becomes really challenging to check if they exist or undefined. At times such mistakes may break the UI as well. If we go for nested checks, the code looks clumsy. The following package comes in handy to solve this issue.

Npm Package: https://www.npmjs.com/package/is-obj-props

Details:

Package name: is-obj-props

A small, yet very useful npm package, which is used to check if an object possess a property or not. Checks for any depth of nested objects

- If the object has the property, it will return the value
- If the property is not present, it returns ‘null’

Installation

$ npm i is-obj-props

How to Use

Install and import is-obj-props

import isObjProps from ‘is-obj-props’;const testObj = {
name: “UserName”,
age: 25,
marks: {
exam1: 98,
exam2: {
part1: 47,
part2: 48
}
}
}

isObjProps is a function which takes 2 arguements:

- Arguement 1 — Type: Object — The Object to be tested (testObj in this case)
- Arguement 2 — Type: Array — Array of Property names. The length of the array denotes the depth of the property in nested object.

The examples will make you understand better

console.log(isObjProps(obj, [“name”]));
//UserName
console.log(isObjProps(obj, [“FirstName”]));
//null
console.log(isObjProps(obj, [“marks”,”exam1"]));
//98
console.log(isObjProps(obj, [“marks”,”exam2"]));
//{part1: 47, part2: 48}
console.log(isObjProps(obj, [“marks”,”exam3"]));
//null
console.log(isObjProps(obj, [“marks”,”exam2",”part2"]));
//48
console.log(isObjProps(obj, [“marks”,”exam2",”part3"]));
//null

This never lets us face undefined error due to object properties again.

Contribution

You are welcome to raise issues and PRs in the [repository](https://github.com/vilvaathibanpb/is-obj-props)

--

--

Vilva Athiban P B

9+ years of Experience in React-Redux / NodeJS / GraphQL / TypeScript. https://vilvaathiban.com