README.md (3447B)
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 # Zip 22 23 > Generate array tuples from input arrays. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var zip = require( '@stdlib/utils/zip' ); 37 ``` 38 39 #### zip( arr1, arr2,...\[, opts] ) 40 41 Returns an `array` of `arrays`, where the ith element (tuple) in the returned `array` contains the ith elements of the input `arrays`. 42 43 ```javascript 44 var zipped = zip( [ 1, 2 ], [ 'a', 'b' ] ); 45 // returns [ [ 1, 'a' ], [ 2, 'b' ] ] 46 ``` 47 48 By default, the returned `array` length is the length of the shortest input `array`. 49 50 ```javascript 51 var zipped = zip( [ 1, 2, 3 ], [ 'a', 'b' ] ); 52 // returns [ [ 1, 'a' ], [ 2, 'b' ] ] 53 ``` 54 55 The function accepts an `options` object with optional properties: 56 57 - **trunc**: `boolean` specifying whether the returned `array` should truncate `arrays` longer than the shortest `array`. Default: `true`. 58 - **fill**: fill value used for unequal length `arrays`. Default: `null`. 59 - **arrays**: `boolean` specifying whether, when provided a single input `array`, the function should interpret the argument as a list of `arrays` to be zipped (i.e., behavior similar to `zip.apply(null, arr)`). Default: `false`. 60 61 To turn off truncation, 62 63 ```javascript 64 var opts = { 65 'trunc': false 66 }; 67 68 var zipped = zip( [ 1, 2, 3 ], [ 'a', 'b' ], opts ); 69 // returns [ [ 1, 'a' ], [ 2, 'b' ], [ 3, null ] ] 70 ``` 71 72 A fill value is included in each tuple for each `array` which does not have an element at the ith index. By default, the fill value is `null`. To specify a different fill value, set the `fill` option. 73 74 ```javascript 75 var opts = { 76 'trunc': false, 77 'fill': '' 78 }; 79 80 var zipped = zip( [ 1, 2, 3 ], [ 'a', 'b' ], opts ); 81 // returns [ [ 1, 'a' ], [ 2, 'b' ], [ 3, '' ] ] 82 ``` 83 84 If the function should interpret a single input `array` as an `array` of `arrays` to be zipped, 85 86 <!-- eslint-disable object-curly-newline --> 87 88 ```javascript 89 var zipped; 90 var arr = [ [ 1, 2 ], [ 'a', 'b' ] ]; 91 92 // Default behavior: 93 zipped = zip( arr ); 94 // returns [ [ [ 1, 2 ] ], [ [ 'a', 'b' ] ] ] 95 96 // Array of arrays: 97 zipped = zip( arr, { 'arrays': true } ); 98 // returns [ [ 1, 'a' ], [ 2, 'b' ] ] 99 ``` 100 101 </section> 102 103 <!-- /.usage --> 104 105 <section class="examples"> 106 107 ## Examples 108 109 <!-- eslint no-undef: "error" --> 110 111 ```javascript 112 var randu = require( '@stdlib/random/base/randu' ); 113 var zip = require( '@stdlib/utils/zip' ); 114 115 var zipped; 116 var len; 117 var y1; 118 var y2; 119 var y3; 120 var x; 121 var i; 122 123 // Simulate some data... 124 x = new Array( 100 ); 125 len = x.length; 126 y1 = new Array( len ); 127 y2 = new Array( len ); 128 y3 = new Array( len ); 129 130 for ( i = 0; i < len; i++ ) { 131 x[ i ] = Date.now(); 132 y1[ i ] = randu() * 100; 133 y2[ i ] = randu() * 100; 134 y3[ i ] = randu(); 135 } 136 137 zipped = zip( x, y1, y2, y3 ); 138 139 console.log( zipped.join( '\n' ) ); 140 ``` 141 142 </section> 143 144 <!-- /.examples --> 145 146 <section class="links"> 147 148 </section> 149 150 <!-- /.links -->