README.md (5059B)
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 # forEach 22 23 > Invoke a function for each element in a collection. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var forEach = require( '@stdlib/utils/for-each' ); 41 ``` 42 43 #### forEach( collection, fcn\[, thisArg ] ) 44 45 Invokes a `function` for each element in a `collection`. 46 47 ```javascript 48 function log( value, index ) { 49 console.log( '%s: %d', index, value ); 50 } 51 52 var arr = [ 1, 2, 3, 4 ]; 53 54 forEach( arr, log ); 55 /* => 56 0: 1 57 1: 2 58 2: 3 59 3: 4 60 */ 61 ``` 62 63 The invoked `function` is provided three arguments: 64 65 - `value`: collection element 66 - `index`: collection index 67 - `collection`: input collection 68 69 Basic support for dynamic collections is provided. Note, however, that index incrementation is monotonically increasing. 70 71 ```javascript 72 function log1( value, index, collection ) { 73 console.log( '%s: %d', index, value ); 74 if ( index === collection.length-1 && collection.length < 10 ) { 75 collection.push( index+2 ); 76 } 77 } 78 79 var arr = [ 1, 2, 3, 4 ]; 80 81 forEach( arr, log1 ); 82 /* => 83 0: 1 84 1: 2 85 2: 3 86 3: 4 87 4: 5 88 5: 6 89 6: 7 90 7: 8 91 8: 9 92 9: 10 93 */ 94 95 function log2( value, index, collection ) { 96 console.log( '%s: %d', index, value ); 97 collection.shift(); 98 } 99 100 arr = [ 1, 2, 3, 4 ]; 101 102 forEach( arr, log2 ); 103 /* => 104 0: 1 105 1: 3 106 */ 107 ``` 108 109 To set the function execution context, provide a `thisArg`. 110 111 ```javascript 112 function sum( value ) { 113 this.sum += value; 114 this.count += 1; 115 } 116 117 var arr = [ 1, 2, 3, 4 ]; 118 119 var context = { 120 'sum': 0, 121 'count': 0 122 }; 123 124 forEach( arr, sum, context ); 125 126 var mean = context.sum / context.count; 127 // returns 2.5 128 ``` 129 130 </section> 131 132 <!-- /.usage --> 133 134 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 135 136 <section class="notes"> 137 138 ## Notes 139 140 - A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). 141 142 - The function differs from [`Array.prototype.forEach`][mdn-array-foreach] in the following ways: 143 144 - The function returns the input `collection`. 145 146 - The function does **not** skip `undefined` elements. 147 148 <!-- eslint-disable no-sparse-arrays --> 149 150 ```javascript 151 function log( value, index ) { 152 console.log( '%s: %s', index, value ); 153 } 154 155 var arr = [ 1, , , 4 ]; 156 157 forEach( arr, log ); 158 /* => 159 0: 1 160 1: undefined 161 2: undefined 162 3: 4 163 */ 164 ``` 165 166 - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution). 167 168 </section> 169 170 <!-- /.notes --> 171 172 <!-- Package usage examples. --> 173 174 <section class="examples"> 175 176 ## Examples 177 178 <!-- eslint no-undef: "error" --> 179 180 ```javascript 181 var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; 182 var forEach = require( '@stdlib/utils/for-each' ); 183 184 function log( value, index, collection ) { 185 console.log( '%s: %d', index, value ); 186 if ( isEven( index ) ) { 187 collection.shift(); 188 } else { 189 collection.push( index+1 ); 190 } 191 } 192 193 var arr; 194 var i; 195 196 arr = new Array( 100 ); 197 for ( i = 0; i < arr.length; i++ ) { 198 arr[ i ] = i; 199 } 200 201 forEach( arr, log ); 202 ``` 203 204 </section> 205 206 <!-- /.examples --> 207 208 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 209 210 <section class="references"> 211 212 </section> 213 214 <!-- /.references --> 215 216 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 217 218 <section class="links"> 219 220 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 221 222 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 223 224 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 225 226 [mdn-array-foreach]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach 227 228 </section> 229 230 <!-- /.links -->