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 |
|
7 | Install with [npm](https://www.npmjs.com/)
|
8 |
|
9 | ```sh
|
10 | $ npm i get-object --save
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | var get = require('get-object');
|
17 |
|
18 | get({a: {b: {c: 'd'}}}, 'a.b.c');
|
19 | //=> {c: 'd'}
|
20 | ```
|
21 |
|
22 | If 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
|
27 | get({a: 'aaa', b: {c: 'd'}}, 'a');
|
28 | //=> {a: 'aaa'}
|
29 |
|
30 | get({a: 'a', b: {c: 'd'}}, 'b.c');
|
31 | //=> {c: 'd'}
|
32 | ```
|
33 |
|
34 | ## get a value from an array
|
35 |
|
36 | ```js
|
37 | get(['a', 'b', 'c'], '0');
|
38 | //=> ['a']
|
39 |
|
40 | get(['a', 'b', 'c'], 2);
|
41 | //=> ['c']
|
42 | ```
|
43 |
|
44 | ## get nested value from an array
|
45 |
|
46 | ```js
|
47 | get({a: ['a', 'b', 'c']}, 'a.0');
|
48 | //=> ['a']
|
49 |
|
50 | get({a: ['a', 'b', 'c']}, 'a.2');
|
51 | //=> ['c']
|
52 |
|
53 | get({a: {b: ['a', 'b', 'c']}}, 'a.b.2');
|
54 | //=> ['c']
|
55 | ```
|
56 |
|
57 | ## support array notation
|
58 |
|
59 | ```js
|
60 | get({a: ['a', 'b', 'c']}, 'a[0]');
|
61 | //=> ['a']
|
62 |
|
63 | get({a: ['a', 'b', 'c']}, 'a[2]');
|
64 | //=> ['c']
|
65 |
|
66 | get({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 |
|
81 | Install dev dependencies:
|
82 |
|
83 | ```sh
|
84 | $ npm i -d && npm test
|
85 | ```
|
86 |
|
87 | ## Contributing
|
88 |
|
89 | Pull 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 |
|
100 | Copyright © 2015 Jon Schlinkert
|
101 | Released 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._
|