README.md (4864B)
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 # Find 22 23 > Find elements in an array-like object that satisfy a test condition. 24 25 <section class="usage"> 26 27 ## Usage 28 29 <!-- eslint-disable stdlib/no-redeclare --> 30 31 ```javascript 32 var find = require( '@stdlib/utils/find' ); 33 ``` 34 35 #### find( arr, \[opts,] clbk ) 36 37 Finds elements in an array-like object that satisfy a test condition. The function accepts two options: `k` and `returns`. 38 39 - **k**: an `integer` which limits the number of elements returned and whose sign determines the direction in which to search. If set to a negative `integer`, the function searches from the last element to the first element. 40 41 - **returns**: specifies the type of result to return and may be one of three options: `indices`, `values`, `*`. 42 43 - **indices**: indicates to return the element indices of those elements satisfying the search condition. 44 - **values**: indicates to return the element values of those elements satisfying the search condition. 45 - **\***: indicates to return both the element indices and values of those elements satisfying the search condition. The returned result is an `array` of `arrays`, where each sub-array is an index-value pair. 46 47 The `callback` is provided three arguments: 48 49 - **element**: the current element 50 - **index**: the current element's index 51 - **array**: the input `array`, `typed array` or `string` 52 53 By default, `k` is the length of `arr` and `returns` is set to `indices`. 54 55 <!-- eslint-disable stdlib/no-redeclare --> 56 57 ```javascript 58 var data = [ 30, 20, 50, 60, 10 ]; 59 60 function greaterThan20( val ) { 61 return val > 20; 62 } 63 64 var vals = find( data, greaterThan20 ); 65 // returns [ 0, 2, 3 ] 66 67 data = 'Hello World'; 68 function isUpperCase( val ) { 69 return /[A-Z]/.test( val ); 70 } 71 72 vals = find( data, isUpperCase ); 73 // returns [ 0, 6 ] 74 ``` 75 76 To limit the number of results and specify that `values` should be returned, 77 78 <!-- eslint-disable stdlib/no-redeclare --> 79 80 ```javascript 81 var data = [ 30, 20, 50, 60, 10 ]; 82 83 var opts = { 84 'k': 2, 85 'returns': 'values' 86 }; 87 88 function condition( val ) { 89 return val > 20; 90 } 91 92 var vals = find( data, opts, condition ); 93 // returns [ 30, 50 ] 94 ``` 95 96 If no `array` elements satisfy the test condition, the function returns an empty `array`. 97 98 <!-- eslint-disable stdlib/no-redeclare --> 99 100 ```javascript 101 var data = [ 30, 20, 50, 60, 10 ]; 102 103 var opts = { 104 'k': 2, 105 'returns': 'values' 106 }; 107 108 function condition( val ) { 109 return val > 1000; 110 } 111 112 var vals = find( data, opts, condition ); 113 // returns [] 114 ``` 115 116 To find the last two values satisfying a search condition, 117 118 <!-- eslint-disable stdlib/no-redeclare --> 119 120 ```javascript 121 var data = [ 30, 20, 50, 60, 10 ]; 122 123 var opts = { 124 'k': -2, 125 'returns': 'values' 126 }; 127 128 function condition( val ) { 129 return val > 20; 130 } 131 132 var vals = find( data, opts, condition ); 133 // returns [ 60, 50 ] 134 ``` 135 136 To explicitly specify that only indices are returned, 137 138 <!-- eslint-disable stdlib/no-redeclare --> 139 140 ```javascript 141 var data = [ 30, 20, 50, 60, 10 ]; 142 143 var opts = { 144 'k': -2, 145 'returns': 'indices' 146 }; 147 148 function condition( val ) { 149 return val > 20; 150 } 151 152 var vals = find( data, opts, condition ); 153 // returns [ 3, 2 ] 154 ``` 155 156 And to return both indices and values as index-value pairs, 157 158 <!-- eslint-disable stdlib/no-redeclare --> 159 160 ```javascript 161 var data = [ 30, 20, 50, 60, 10 ]; 162 163 var opts = { 164 'k': -2, 165 'returns': '*' 166 }; 167 168 function condition( val ) { 169 return val > 20; 170 } 171 172 var vals = find( data, opts, condition ); 173 // returns [ [3, 60], [2, 50] ] 174 ``` 175 176 </section> 177 178 <!-- /.usage --> 179 180 <section class="examples"> 181 182 ## Examples 183 184 <!-- eslint-disable stdlib/no-redeclare --> 185 186 <!-- eslint no-undef: "error" --> 187 188 ```javascript 189 var round = require( '@stdlib/math/base/special/round' ); 190 var randu = require( '@stdlib/random/base/randu' ); 191 var find = require( '@stdlib/utils/find' ); 192 193 var data; 194 var opts; 195 var vals; 196 var i; 197 198 // Simulate the data... 199 data = new Array( 100 ); 200 201 for ( i = 0; i < data.length; i++ ) { 202 data[ i ] = round( randu*100 ); 203 } 204 205 // Find the first 10 values greater than 25... 206 opts = { 207 'k': 10, 208 'returns': '*' 209 }; 210 211 function condition( val ) { 212 return val > 25; 213 } 214 215 vals = find( data, opts, condition ); 216 console.log( vals.join( '\n' ) ); 217 ``` 218 219 </section> 220 221 <!-- /.examples --> 222 223 <section class="links"> 224 225 </section> 226 227 <!-- /.links -->