https://github.com/capaj/object-resolve-path
a simple utility function for getting a value at a path from an object with all the usecases properly covered
https://github.com/capaj/object-resolve-path
Last synced: 11 months ago
JSON representation
a simple utility function for getting a value at a path from an object with all the usecases properly covered
- Host: GitHub
- URL: https://github.com/capaj/object-resolve-path
- Owner: capaj
- License: mit
- Created: 2015-07-30T14:56:23.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-12-28T14:17:02.000Z (over 7 years ago)
- Last Synced: 2024-12-10T07:12:39.978Z (over 1 year ago)
- Language: JavaScript
- Size: 24.4 KB
- Stars: 66
- Watchers: 4
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# object-resolve-path
a simple utility function for getting a value at a path from an object with all the usecases properly covered.
Based on Path pseodoclass from https://github.com/Polymer/observe-js/blob/7e94bb14d7c44b221af7bcc874cf9898f26747d8/src/observe.js#L305
## Install
```
npm i object-resolve-path -S
```
## Usage
```javascript
const resolvePath = require('object-resolve-path');
resolvePath(someObject, 'a.b[0]'); //returns first property from b from a from someObject
resolvePath(someObject, 'a["b-a"][0]'); //this works as well, thanks to the parser/statemachine
//with variables
const someId = 'some-id'
resolvePath(someObject, `a.b['${someId}']`); //return key 'some-id' from the nested a.b object
```
## Typical usecase?
Most other libraries for accessing deeply nested properties of an object don't work with bracket syntax. This one does work with bracket syntax.
For thorough description, check the [tests](https://github.com/capaj/object-resolve-path/blob/master/test/object-resolve-path.spec.js).
### Similar modules:
https://www.npmjs.com/package/lodash.get (works for both but much much slower, doesn't throw when path is not valid, object-resolve-path does)
https://github.com/deoxxa/dotty (works only for dots)
https://github.com/Ntran013/dot-access (works only for dots)
https://github.com/substack/js-traverse (much more complex and useful)
## Benchmarks
```
node benchmark.js
lodash.get x 2,253,484 ops/sec ±0.83% (94 runs sampled)
object-resolve-path x 39,876,349 ops/sec ±0.82% (92 runs sampled)
Fastest is object-resolve-path
```
[](https://github.com/feross/standard)