README.md (3309B)
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 # mapKeys 22 23 > Map keys from one object to a new object having the same values. 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 mapKeys = require( '@stdlib/utils/map-keys' ); 41 ``` 42 43 #### mapKeys( obj, transform ) 44 45 Map keys from one `object` to a new `object` having the same values. 46 47 ```javascript 48 function transform( key, value ) { 49 return key + value; 50 } 51 52 var obj1 = { 53 'a': 1, 54 'b': 2 55 }; 56 57 var obj2 = mapKeys( obj1, transform ); 58 // returns { 'a1': 1, 'b2': 2 } 59 ``` 60 61 The `transform` function is provided three arguments: 62 63 - `key`: object key 64 - `value`: object value corresponding to `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 **own** properties. Hence, the function does **not** map inherited properties. 79 - The function **shallow** copies key values. 80 - The value returned by a `transform` function should be a value which can be serialized as an `object` key. 81 82 </section> 83 84 <!-- /.notes --> 85 86 <!-- Package usage examples. --> 87 88 <section class="examples"> 89 90 ## Examples 91 92 <!-- eslint no-undef: "error" --> 93 94 ```javascript 95 var mapKeys = require( '@stdlib/utils/map-keys' ); 96 97 function transform( key, value ) { 98 return key + ':' + value; 99 } 100 101 var obj1 = { 102 'a': 'beep', 103 'b': 'boop', 104 'c': 'foo', 105 'd': 'bar' 106 }; 107 108 var obj2 = mapKeys( obj1, transform ); 109 110 console.dir( obj2 ); 111 // => { 'a:beep': 'beep', 'b:boop': 'boop', 'c:foo': 'foo', 'd:bar': 'bar' } 112 ``` 113 114 </section> 115 116 <!-- /.examples --> 117 118 <!-- 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. --> 119 120 <section class="references"> 121 122 </section> 123 124 <!-- /.references --> 125 126 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 127 128 <section class="links"> 129 130 [ecma-262-for-in]: http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4 131 132 </section> 133 134 <!-- /.links -->