README.md (4882B)
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 Pluck 22 23 > Extract a nested property value from each element of an object array. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var deepPluck = require( '@stdlib/utils/deep-pluck' ); 37 ``` 38 39 #### deepPluck( arr, path\[, options] ) 40 41 Extracts a nested property value from each element of an object `array` based on a key `path`. 42 43 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 44 45 ```javascript 46 var arr = [ 47 { 'a': { 'b': { 'c': 1 } } }, 48 { 'a': { 'b': { 'c': 2 } } } 49 ]; 50 51 var out = deepPluck( arr, 'a.b.c' ); 52 // returns [ 1, 2 ] 53 ``` 54 55 A key `path` may be specified as either a `string` or as an `array`. 56 57 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 58 59 ```javascript 60 var arr = [ 61 { 'a': [ 0, 1, 2 ] }, 62 { 'a': [ 3, 4, 5 ] } 63 ]; 64 65 var out = deepPluck( arr, [ 'a', 1 ] ); 66 // returns [ 1, 4 ] 67 ``` 68 69 The function accepts the following `options`: 70 71 - **copy**: `boolean` indicating whether to return a new data structure. Default: `true`. 72 - **sep**: key path [separator][@stdlib/utils/deep-get]. Default: `'.'`. 73 74 By default, the function returns a new data structure. To mutate the input data structure (e.g., when input values can be discarded or when optimizing memory usage), set the `copy` option to `false`. 75 76 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 77 78 ```javascript 79 var arr = [ 80 { 'a': { 'b': { 'c': 1 } } }, 81 { 'a': { 'b': { 'c': 2 } } } 82 ]; 83 84 var out = deepPluck( arr, 'a.b.c', { 'copy': false } ); 85 // returns [ 1, 2 ] 86 87 var bool = ( arr[ 0 ] === out[ 0 ] ); 88 // returns true 89 ``` 90 91 The default key `path` separator is `.`. To specify an alternative separator, set the `sep` option. 92 93 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 94 95 ```javascript 96 var arr = [ 97 { 'a': { 'b': { 'c': 1 } } }, 98 { 'a': { 'b': { 'c': 2 } } } 99 ]; 100 101 var out = deepPluck( arr, 'a|b|c', { 'sep': '|' } ); 102 // returns [ 1, 2 ] 103 ``` 104 105 </section> 106 107 <!-- /.usage --> 108 109 <section class="notes"> 110 111 - If a key path does **not** exist, the function sets the plucked value as `undefined`. 112 113 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 114 115 ```javascript 116 var arr = [ 117 { 'a': { 'b': { 'c': 1 } } }, 118 null, 119 void 0, 120 { 'a': { 'b': { 'c': 2 } } } 121 ]; 122 123 var out = deepPluck( arr, 'a.b.c' ); 124 // returns [ 1, undefined, undefined, 2 ] 125 ``` 126 127 - Extracted values are **not** cloned. 128 129 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 130 131 ```javascript 132 var arr = [ 133 { 'a': { 'b': { 'c': 2 } } }, 134 { 'a': { 'b': { 'c': 3 } } } 135 ]; 136 137 var out = deepPluck( arr, 'a.b' ); 138 // returns [ { 'c': 2 }, { 'c': 3 } ] 139 140 var bool = ( arr[ 0 ].a.b === out[ 0 ] ); 141 // returns true 142 ``` 143 144 To prevent subsequent unintended mutation, use [copy][@stdlib/utils/copy]. 145 146 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 147 148 ```javascript 149 var copy = require( '@stdlib/utils/copy' ); 150 151 var arr = [ 152 { 'a': { 'b': { 'c': 2 } } }, 153 { 'a': { 'b': { 'c': 3 } } } 154 ]; 155 156 var out = deepPluck( arr, 'a.b' ); 157 // returns [ { 'c': 2 }, { 'c': 3 } ] 158 159 // Perform a deep copy: 160 out = copy( out ); 161 162 var bool = ( arr[ 0 ].a.b === out[ 0 ] ); 163 // returns false 164 ``` 165 166 </section> 167 168 <!-- /.notes --> 169 170 <section class="examples"> 171 172 ## Examples 173 174 <!-- eslint no-undef: "error" --> 175 176 ```javascript 177 var randu = require( '@stdlib/random/base/randu' ); 178 var round = require( '@stdlib/math/base/special/round' ); 179 var deepPluck = require( '@stdlib/utils/deep-pluck' ); 180 181 var arr; 182 var out; 183 var tmp; 184 var i; 185 186 arr = new Array( 100 ); 187 for ( i = 0; i < arr.length; i++ ) { 188 tmp = { 189 'a': { 190 'b': { 191 'c': { 192 'd': null 193 } 194 } 195 } 196 }; 197 tmp.a.b.c.d = round( randu()*100.0 ); 198 arr[ i ] = tmp; 199 } 200 201 // Pluck the deeply nested values: 202 out = deepPluck( arr, 'a.b.c.d' ); 203 console.log( out ); 204 ``` 205 206 </section> 207 208 <!-- /.examples --> 209 210 <section class="links"> 211 212 [@stdlib/utils/deep-get]: https://www.npmjs.com/package/@stdlib/utils/tree/main/deep-get 213 214 [@stdlib/utils/copy]: https://www.npmjs.com/package/@stdlib/utils/tree/main/copy 215 216 </section> 217 218 <!-- /.links -->