delve

Delve recursively into a value to retrieve a property; without erroring.

delve

Delve recursively into a value to retrieve a property; without erroring.

It sucks to have to do if ( obj && obj.prop && obj.prop.secondProp ) { ... }.

var delve = require('delve')
 
var o = { x: { y: { z: 'my val' } } }
 
delve(o, 'x.y') //= { z: 'my val' } 
delve(o, 'x.y.z') //= 'my val' 
delve(o, 'x.y.z.foo') //= undefined 
delve(undefined, 'x.y.z.foo') //= undefined 

For more examples, see the tests/delve-test.js

npm install delve

Download src/delve.js, and include it as a script tag.