@stdlib/array
Version:
Arrays.
142 lines (84 loc) • 3.55 kB
Markdown
<!--
@license Apache-2.0
Copyright (c) 2024 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
# convertSame
> Convert an array to the same [data type][@stdlib/array/dtypes] as a second input array.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
<section class="intro">
</section>
<!-- /.intro -->
<!-- Package usage documentation. -->
<section class="usage">
## Usage
```javascript
var convertSame = require( '@stdlib/array/convert-same' );
```
#### convertSame( x, y )
Converts an array to the same [data type][@stdlib/array/dtypes] as a second input array.
```javascript
var Float32Array = require( '@stdlib/array/float32' );
var x = [ 1.0, 2.0, 3.0 ];
var y = new Float32Array( 0 );
var out = convertSame( x, y );
// returns <Float32Array>[ 1.0, 2.0, 3.0 ]
```
</section>
<!-- /.usage -->
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">
</section>
<!-- /.notes -->
<!-- Package usage examples. -->
<section class="examples">
## Examples
<!-- eslint-disable stdlib/new-cap-error -->
<!-- eslint no-undef: "error" -->
```javascript
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var dtypes = require( '@stdlib/array/dtypes' );
var ctors = require( '@stdlib/array/ctors' );
var convertSame = require( '@stdlib/array/convert-same' );
// Create a generic array:
var arr = filledarrayBy( 5, 'generic', discreteUniform( -100, 100 ) );
// Get a list of array data types:
var DTYPES = dtypes();
// Convert the generic array to each array data type:
var out;
var i;
for ( i = 0; i < DTYPES.length; i++ ) {
out = convertSame( arr, new ( ctors( DTYPES[ i ] ) )( 0 ) );
console.log( out );
}
```
</section>
<!-- /.examples -->
<!-- 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. -->
<section class="references">
</section>
<!-- /.references -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
* * *
## See Also
- <span class="package-name">[`@stdlib/array/convert`][@stdlib/array/convert]</span><span class="delimiter">: </span><span class="description">convert an array to an array of a different data type.</span>
</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="links">
<!-- <related-links> -->
[@stdlib/array/convert]: https://github.com/stdlib-js/array/tree/main/convert
[@stdlib/array/dtypes]: https://github.com/stdlib-js/array/tree/main/dtypes
<!-- </related-links> -->
</section>
<!-- /.links -->