UNPKG

3 kBMarkdownView Raw
1# get-object [![NPM version](https://badge.fury.io/js/get-object.svg)](http://badge.fury.io/js/get-object)
2
3> Get a property from an object using dot (object path) notation.
4
5## Install
6
7Install with [npm](https://www.npmjs.com/)
8
9```sh
10$ npm i get-object --save
11```
12
13## Usage
14
15```js
16var get = require('get-object');
17
18get({a: {b: {c: 'd'}}}, 'a.b.c');
19//=> {c: 'd'}
20```
21
22If you want only the value, not key/value, use [get-value](https://github.com/jonschlinkert/get-value).
23
24## get a value from an object
25
26```js
27get({a: 'aaa', b: {c: 'd'}}, 'a');
28//=> {a: 'aaa'}
29
30get({a: 'a', b: {c: 'd'}}, 'b.c');
31//=> {c: 'd'}
32```
33
34## get a value from an array
35
36```js
37get(['a', 'b', 'c'], '0');
38//=> ['a']
39
40get(['a', 'b', 'c'], 2);
41//=> ['c']
42```
43
44## get nested value from an array
45
46```js
47get({a: ['a', 'b', 'c']}, 'a.0');
48//=> ['a']
49
50get({a: ['a', 'b', 'c']}, 'a.2');
51//=> ['c']
52
53get({a: {b: ['a', 'b', 'c']}}, 'a.b.2');
54//=> ['c']
55```
56
57## support array notation
58
59```js
60get({a: ['a', 'b', 'c']}, 'a[0]');
61//=> ['a']
62
63get({a: ['a', 'b', 'c']}, 'a[2]');
64//=> ['c']
65
66get({a: {b: ['a', 'b', {c: 'd'}]}}, 'a.b[2].c');
67//=> {c: 'd'}
68```
69
70## Related projects
71
72* [del-value](https://www.npmjs.com/package/del-value): Delete deeply nested value from an object using dot notation like ` a.b.c.x` and return the… [more](https://www.npmjs.com/package/del-value) | [homepage](https://github.com/tunnckocore/del-value)
73* [get-value](https://www.npmjs.com/package/get-value): Use property paths (` a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value)
74* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value)
75* [union-value](https://www.npmjs.com/package/union-value): Set an array of unique values as the property of an object. Supports setting deeply… [more](https://www.npmjs.com/package/union-value) | [homepage](https://github.com/jonschlinkert/union-value)
76* [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value)
77* [upsert-value](https://www.npmjs.com/package/upsert-value): Update or set nested values and any intermediaries with dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/doowb/upsert-value)
78
79## Running tests
80
81Install dev dependencies:
82
83```sh
84$ npm i -d && npm test
85```
86
87## Contributing
88
89Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/get-object/issues/new).
90
91## Author
92
93**Jon Schlinkert**
94
95+ [github/jonschlinkert](https://github.com/jonschlinkert)
96+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
97
98## License
99
100Copyright © 2015 Jon Schlinkert
101Released under the MIT license.
102
103***
104
105_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 27, 2015._