# get-deep-value

> Safely retrieve a Javascrpt value from a nested array and/or object

Latest version **1.1.4** (published 2020-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install get-deep-value
pnpm add get-deep-value
yarn add get-deep-value
bun add get-deep-value
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2020-07-17 |
| First published | 2019-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jesse Tessmer |
| Maintainers | jtessmer |

## Links

- npm: https://www.npmjs.com/package/get-deep-value
- Repository: https://github.com/JTessmer/get-deep-value
- Homepage: https://github.com/JTessmer/get-deep-value#readme
- Issues: https://github.com/JTessmer/get-deep-value/issues
- npm.io page: https://npm.io/package/get-deep-value

## Recent versions

- 1.1.4 (latest) — 2020-07-17
- 1.1.3 — 2020-07-17
- 1.1.2 — 2020-04-23
- 1.1.1 — 2020-02-05
- 1.1.0 — 2019-10-27
- 1.0.2 — 2019-05-08
- 1.0.1 — 2019-05-04
- 1.0.0 — 2019-05-04

## README

# get-deep-value
This is a simple Javascript utility which will quickly and safely retrieve a value from a nested array and/or object.

## Installation

First, use npm (or yarn) to install:
```
npm install get-deep-value
```

Then simply import the default function:
```js
import getDeepValue from 'get-deep-value'
```

## Use

`getDeepValue` takes three arguments:

| Argument | Type            | Description                                                                  |
|----------|-----------------|------------------------------------------------------------------------------|
| base     | object or array | This is the object you want to traverse                                      |
| path     | string          | This describes the traversal path                                            |
| fallback | any             | [optional] This is the value to return if traversal fails; default is `null` |


## Examples

We'll use this object for the base of all of the below examples:
```js
const base = {
    my: {
        object: {
            value: 'foo'
        },
        array: [
            {
                value: 'bar'
            }
        ]
    }
}
```

You can access a nested object value:
```js
getDeepValue(base, 'my.object.value')               // foo
```

Or a nested array value:
```js
getDeepValue(base, 'my.array[0].value')             // bar
```

You can also use a dynamic path:
```js
const typeName = 'object'
const arrayItem = 0

getDeepValue(base, `my.${typeName}.value`)          // foo
getDeepValue(base, `my.array[${arrayItem}].value`)  // bar
```

All of these will safely return `null`:
```js
getDeepValue(base, 'no.object.value')               // null
getDeepValue(base, 'my.nonexistant.value')          // null
getDeepValue(base, 'my.object.nothing')             // null
getDeepValue(base, 'my.array[1].value')             // null
```

Optionally, you can specify a fallback value other than `null`:
```js
getDeepValue(base, 'no.object.value', 'baz')        // baz
```

---
_Source: https://npm.io/package/get-deep-value · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
