@jamesnimlos/safe-get
Version:
A small safe accessor that uses reduce to prevent 'Cannot read property of undefined' errors
23 lines (21 loc) • 643 B
Markdown
***
* npm install safe-get -save
```JavaScript
const _ = require ('safe-get');
const myObject = {
foo: {
bar: {
baz: ['sget','protects','me']
}
}
}
let result = _(myObject,'foo.bar.baz[1]'); // result === 'protects'
result = _(myObject,'foo.missingProperty.this.could.go.on.forever'); // result = undefined - no Exception hooray!
```
See this [blog post](http://wakecoder.com) on [wakecoder.com](http://wakecoder.com) for more detail.
Also, see tests.js for more examples.