@mzvonar/getin
Version:
Get value from object by path. Undefined-safe and accepts default value
43 lines (29 loc) • 1.15 kB
Markdown
getIn [](https://travis-ci.org/mzvonar/getIn) [](https://coveralls.io/github/mzvonar/getIn?branch=master) [](https://badge.fury.io/js/%40mzvonar%2Fgetin)
=========
Get value from object by path. Path can be string or array (e.g. ['user', 'profile', 'gender']).
If any value in path is undefined then undefined is returned or defaultValue if provided.
## Installation
`npm install @mzvonar/getin`
## Usage
```javascript
const getIn = require('@mzvonar/getin');
const context = {
user: {
profile: {
gender: 'female'
}
}
};
const gender = getIn(context, ['user', 'profile', 'gender']);
```
gender is `female`
```javascript
const country = getIn(context, ['user', 'address', 'country']);
```
country is `undefined`
```javascript
const verified = getIn(context, ['user', 'profile', 'verified'], false);
```
verified is `false`
`npm test`