https://github.com/joe-re/object-dig
This allow you to use method like Ruby's hash#dig in JavaScript.
https://github.com/joe-re/object-dig
javascript
Last synced: about 1 year ago
JSON representation
This allow you to use method like Ruby's hash#dig in JavaScript.
- Host: GitHub
- URL: https://github.com/joe-re/object-dig
- Owner: joe-re
- License: mit
- Created: 2016-01-27T14:14:14.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-05-17T18:15:06.000Z (about 8 years ago)
- Last Synced: 2024-10-14T02:35:54.904Z (over 1 year ago)
- Topics: javascript
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 22
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# object-dig
[](https://travis-ci.org/joe-re/object-dig)
This allow you to use method like Ruby's hash#dig in JavaScript.
> http://ruby-doc.org/core-2.3.0_preview1/Hash.html#method-i-dig
>> Retrieves the value object corresponding to the each key objects repeatedly.
# Install
```
$ npm install --save object-dig
```
# Usage
```js
var dig = require('object-dig');
var object = { a: { b: { c: 'c' } } };
dig(object, 'a', 'b');
// => { c: 'c' }
dig(object, 'a', 'b', 'c');
// => 'c'
dig(object, 'a', 'unknownProp', 'c');
// =>undefined
```
and you can give function object to dig.
Function object's argument is result of just before evaluating.
```js
dig(object, 'a', 'b', 'c', (val) => `${val} was changed`);
// => 'c was changed'
dig(object, 'a', 'b', 'c', (val) => `${val} was changed`, (val) => `${val} more`);
// => 'c was changed more'
```
# License
MIT © [joe-re](https://github.com/joe-re)