convert

Convert an array to an array of a different data type.

Usage

var convert = require( '@stdlib/array/convert' );

convert( arr, dtype )

Converts an array to an array of a different data type.

var arr = [ 1.0, 2.0, 3.0 ];
var out = convert( arr, 'float32' );
// returns <Float32Array>[ 1.0, 2.0, 3.0 ]

Examples

var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var dtypes = require( '@stdlib/array/dtypes' );
var convert = require( '@stdlib/array/convert' );

// 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 = convert( arr, DTYPES[ i ] );
    console.log( out );
}
Did you find this page helpful?