README.md (3136B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Deep Get 22 23 > Get a nested property value. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var deepGet = require( '@stdlib/utils/deep-get' ); 31 ``` 32 33 #### deepGet( obj, path\[, options] ) 34 35 Returns a nested property value. 36 37 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 38 39 ```javascript 40 var obj = { 'a': { 'b': { 'c': 'd' } } }; 41 42 var val = deepGet( obj, 'a.b.c' ); 43 // returns 'd' 44 ``` 45 46 For `paths` including `arrays`, specify the numeric index. 47 48 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 49 50 ```javascript 51 var arr = [ 52 { 'a': [ {'x': 5} ] }, 53 { 'a': [ {'x': 10} ] } 54 ]; 55 56 var val = deepGet( arr, '1.a.0.x' ); 57 // returns 10 58 ``` 59 60 The key `path` may be specified as either a delimited `string` or a key `array`. 61 62 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 63 64 ```javascript 65 var obj = { 'a': { 'b': { 'c': 'd' } } }; 66 67 var val = deepGet( obj, [ 'a', 'b', 'c' ] ); 68 // returns 'd' 69 ``` 70 71 The function accepts the following `options`: 72 73 - **sep**: key path separator. Default: `'.'`. 74 75 By default, the function assumes `dot` separated key values. To specify an alternative separator, set the `sep` option. 76 77 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 78 79 ```javascript 80 var obj = { 'a': { 'b': { 'c': 'd' } } }; 81 82 var val = deepGet( obj, 'a/b/c', { 83 'sep': '/' 84 }); 85 // returns 'd' 86 ``` 87 88 #### deepGet.factory( path\[, options] ) 89 90 Creates a reusable deep get function. The factory method ensures a `deepGet` function is configured identically by using the same set of provided `options`. 91 92 ```javascript 93 var dget = deepGet.factory( 'a/b/c', { 94 'sep': '/' 95 }); 96 ``` 97 98 #### dget( obj ) 99 100 Returns a nested property value. 101 102 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 103 104 ```javascript 105 var dget = deepGet.factory( 'a.b.c' ); 106 107 var obj = { 'a': { 'b': { 'c': 'd' } } }; 108 109 var val = dget( obj ); 110 // returns 'd' 111 ``` 112 113 </section> 114 115 <!-- /.usage --> 116 117 <section class="examples"> 118 119 ## Examples 120 121 <!-- eslint no-undef: "error" --> 122 123 ```javascript 124 var randu = require( '@stdlib/random/base/randu' ); 125 var deepGet = require( '@stdlib/utils/deep-get' ); 126 127 var data; 128 var keys; 129 var val; 130 var i; 131 132 data = new Array( 100 ); 133 for ( i = 0; i < data.length; i++ ) { 134 data[ i ] = { 135 'x': Date.now(), 136 'y': [ randu(), randu(), i ] 137 }; 138 } 139 140 keys = [ 0, 'y', 2 ]; 141 for ( i = 0; i < data.length; i++ ) { 142 keys[ 0 ] = i; 143 val = deepGet( data, keys ); 144 console.log( val ); 145 } 146 ``` 147 148 </section> 149 150 <!-- /.examples --> 151 152 <section class="links"> 153 154 </section> 155 156 <!-- /.links -->