https://github.com/eddmann/get-from
Safely returns the value(s) in a nested object/array structure.
https://github.com/eddmann/get-from
Last synced: 11 months ago
JSON representation
Safely returns the value(s) in a nested object/array structure.
- Host: GitHub
- URL: https://github.com/eddmann/get-from
- Owner: eddmann
- Created: 2018-10-23T12:17:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-24T08:39:31.000Z (over 7 years ago)
- Last Synced: 2025-06-07T05:06:58.458Z (about 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# get-from
[](https://travis-ci.org/eddmann/get-from)
Safely returns the value(s) in a nested object/array structure.
Having to do boilerplate checks such as `obj["a"] && obj["a"]["b"] && obj["a"]["b"]["c"]` complicates code!
Instead all you need to do is provide you desired final path and optional default value.
## Usage
```js
import get from "get-from";
// multi-level objects
get("a", { a: "🎉" }); // => "🎉"
get("a.b", { a: { b: "🎉" } }); // => "🎉"
get("a.c", { a: { b: "🎉" } }); // => undefined
get("a.c", { a: { b: "🎉" } }, "😥"); // => "😥"
// array index access
get("a.0", { a: [{ b: "🎉" }] }); // => { b: "🎉" }
// multi-map array access
get("a[].b", { a: [{ b: "🎉" }, { b: "🎂" }] }); // => ["🎉", "🎂"]
get("a[].b[].c", { a: [{ b: [{ c: "🎉" }] }, { b: [{ c: "🎂" }] }] }); // => [["🎉"], ["🎂"]]
// all together!
get("a.b[].c.0.d", { a: { b: [{ c: [{ d: "🎉" }, { e: "🎂" }] }] } }); // => ["🎉"]
```