README.md (3806B)
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 # forOwn 22 23 > Invoke a function for each own enumerable property of an object. 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 forOwn = require( '@stdlib/utils/for-own' ); 41 ``` 42 43 #### forOwn( obj, fcn\[, thisArg ] ) 44 45 Invokes a `function` for each own enumerable property of an `object`. 46 47 ```javascript 48 function log( value, key ) { 49 console.log( '%s: %d', key, value ); 50 } 51 52 var obj = { 53 'a': 1, 54 'b': 2, 55 'c': 3, 56 'd': 4 57 }; 58 59 forOwn( obj, log ); 60 /* e.g., => 61 a: 1 62 b: 2 63 c: 3 64 d: 4 65 */ 66 ``` 67 68 The invoked `function` is provided three arguments: 69 70 - `value`: object property value 71 - `key`: object property 72 - `obj`: the input object 73 74 To terminate iteration before visiting all properties, the provided function must explicitly return `false`. 75 76 ```javascript 77 function log( value, key ) { 78 console.log( '%s: %d', key, value ); 79 return false; 80 } 81 82 var obj = { 83 'a': 1, 84 'b': 2, 85 'c': 3, 86 'd': 4 87 }; 88 89 forOwn( obj, log ); 90 // e.g., => a: 1 91 ``` 92 93 To set the function execution context, provide a `thisArg`. 94 95 ```javascript 96 function sum( value ) { 97 this.sum += value; 98 this.count += 1; 99 } 100 101 var obj = { 102 'a': 1, 103 'b': 2, 104 'c': 3, 105 'd': 4 106 }; 107 108 var context = { 109 'sum': 0, 110 'count': 0 111 }; 112 113 forOwn( obj, sum, context ); 114 115 var mean = context.sum / context.count; 116 // returns 2.5 117 ``` 118 119 </section> 120 121 <!-- /.usage --> 122 123 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 124 125 <section class="notes"> 126 127 ## Notes 128 129 - The function returns the input `object`. 130 - The function determines the list of own enumerable properties **before** invoking the provided function. Hence, any modifications made to the input `object` **after** calling this function (such as adding and removing properties) will **not** affect the list of visited properties. 131 - Property iteration order is **not** guaranteed. 132 133 </section> 134 135 <!-- /.notes --> 136 137 <!-- Package usage examples. --> 138 139 <section class="examples"> 140 141 ## Examples 142 143 <!-- eslint no-undef: "error" --> 144 145 ```javascript 146 var fromCodePoint = require( '@stdlib/string/from-code-point' ); 147 var forOwn = require( '@stdlib/utils/for-own' ); 148 149 function update( value, key, obj ) { 150 console.log( '%s: %d', key, value ); 151 obj[ key ] *= value; 152 } 153 154 var obj; 155 var key; 156 var i; 157 158 obj = {}; 159 for ( i = 0; i < 26; i++ ) { 160 key = fromCodePoint( 97 + i ); 161 obj[ key ] = i; 162 } 163 164 forOwn( obj, update ); 165 console.log( obj ); 166 ``` 167 168 </section> 169 170 <!-- /.examples --> 171 172 <!-- 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. --> 173 174 <section class="references"> 175 176 </section> 177 178 <!-- /.references --> 179 180 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 181 182 <section class="links"> 183 184 </section> 185 186 <!-- /.links -->