Skip to content

ajstocchetti/hasDeepProperty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hasDeepProperty

Check if object has deep/nested properties.

Checks if a nested property exists. Rather than check write out the full path for each level you want to check, now you can check the full path all at once

// old messy way
if(obj.someProperty
    && obj.someProperty.anotherProp
    && obj.someProperty.anotherProp.moreProps
    && obj.someProperty.anotherProp.moreProps.youGetThePicture) {
    // do something
}

// new clean way
var hasDeepProperty = require('has-deep-property');
if(hasDeepProperty(obj, ['someProperty', 'anotherProp', 'moreProps', 'youGetThePicture'])) {
    // do something
}

Installation

$ npm install --save hasDeepProperty

API

hasDeepProperty takes two parameters:

  • the object to check
  • an array of properties to check

and returns true or false depending on whether the full path of properties exists

var hasDeepProperty = require('has-deep-property');

var obj = {
    foo: {
        bar: {
            baz: 'exists'
        }
    }
}

hasDeepProperty(obj, ['foo']); // true
hasDeepProperty(obj, ['foo', 'bar', 'baz']); // true
hasDeepProperty(obj, ['foo', 'buzz']); // false
hasDeepProperty(obj, ['foo', 'bar', 'baz', 'exists']); // false - exists is a value, not a property/key name

About

check if object has deep property

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published