Copy a part of a collection to another location in the same collection.
var copyWithin = require( '@stdlib/utils/copy-within' );
Copies a part of a collection
to another location in the same collection
. A collection
may be either an Array
, Typed Array
, or an array-like Object
(i.e., an Object
having a valid writable length
property).
var arr = [ 1, 2, 3, 4, 5 ];
var out = copyWithin( arr, 0, 2, 4 );
// returns [ 3, 4, 3, 4, 5 ]
var bool = ( out === arr );
// returns true
var Float64Array= require( '@stdlib/array/float64' );
var copyWithin = require( '@stdlib/utils/copy-within' );
var bool;
var arr;
var out;
arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
out = copyWithin( arr, 0, 2, 4 );
// returns <Float64Array>[ 3.0, 4.0, 3.0, 4.0, 5.0 ]
bool = ( out === arr );
// returns true
arr = [ 1, 2, 3, 4, 5 ];
out = copyWithin( arr, 0, 3, 4 );
// returns [ 4, 2, 3, 4, 5 ]
bool = ( out === arr );
// returns true
out = copyWithin( arr, 1, 3 );
// returns [ 4, 4, 5, 4, 5 ]
bool = ( out === arr );
// returns true