README.md (3238B)
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 # mapValues 22 23 > Map values from one object to a new object having the same keys. 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 mapValues = require( '@stdlib/utils/map-values' ); 41 ``` 42 43 #### mapValues( obj, transform ) 44 45 Map values from one `object` to a new `object` having the same keys. 46 47 ```javascript 48 function transform( value, key ) { 49 return key + value; 50 } 51 52 var obj1 = { 53 'a': 1, 54 'b': 2 55 }; 56 57 var obj2 = mapValues( obj1, transform ); 58 // returns { 'a': 'a1', 'b': 'b2' } 59 ``` 60 61 The `transform` function is provided three arguments: 62 63 - `value`: object value corresponding to `key` 64 - `key`: object key 65 - `obj`: the input object 66 67 </section> 68 69 <!-- /.usage --> 70 71 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 72 73 <section class="notes"> 74 75 ## Notes 76 77 - Key iteration order is **not** guaranteed, as `object` key enumeration is not specified according to the [ECMAScript specification][ecma-262-for-in]. In practice, however, most engines use insertion order to sort an `object`'s keys, thus allowing for deterministic return values. 78 - The function only maps values assigned to **own** properties. Hence, the function does **not** map values for inherited properties. 79 - The function **shallow** copies key values. 80 81 </section> 82 83 <!-- /.notes --> 84 85 <!-- Package usage examples. --> 86 87 <section class="examples"> 88 89 ## Examples 90 91 <!-- eslint no-undef: "error" --> 92 93 ```javascript 94 var mapValues = require( '@stdlib/utils/map-values' ); 95 96 function transform( value, key ) { 97 return key + ':' + value; 98 } 99 100 var obj1 = { 101 'a': 'beep', 102 'b': 'boop', 103 'c': 'foo', 104 'd': 'bar' 105 }; 106 107 var obj2 = mapValues( obj1, transform ); 108 109 console.dir( obj2 ); 110 // => { 'a': 'a:beep', 'b': 'b:boop', 'c': 'c:foo', 'd': 'd:bar' } 111 ``` 112 113 </section> 114 115 <!-- /.examples --> 116 117 <!-- 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. --> 118 119 <section class="references"> 120 121 </section> 122 123 <!-- /.references --> 124 125 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 126 127 <section class="links"> 128 129 [ecma-262-for-in]: http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4 130 131 </section> 132 133 <!-- /.links -->