@stdlib/array-base-fancy-slice
v0.3.1
Published
Return a shallow copy of a portion of an array.
Downloads
274
Readme
slice
Return a shallow copy of a portion of an array.
Installation
npm install @stdlib/array-base-fancy-slice
Usage
var slice = require( '@stdlib/array-base-fancy-slice' );
slice( x, s, strict )
Returns a shallow copy of a portion of an array.
var Slice = require( '@stdlib/slice-ctor' );
var x = [ 1, 2, 3, 4, 5, 6 ];
var s = new Slice( 1, 4 );
var out = slice( x, s, false );
// returns [ 2, 3, 4 ]
var bool = ( out === x );
// returns false
The function supports the following parameters:
- x: input array.
- s: slice object.
- strict: boolean indicating whether to enforce strict bounds checking.
Notes
- If provided an array-like object having an unknown data type, the function copies input array elements to a new generic array.
Examples
var zeroTo = require( '@stdlib/array-zero-to' );
var Slice = require( '@stdlib/slice-ctor' );
var slice = require( '@stdlib/array-base-fancy-slice' );
var x = zeroTo( 10, 'generic' );
// returns [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
var s = new Slice();
var y = slice( x, s, false );
// returns [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
s = new Slice( null, null, -2 );
y = slice( x, s, false );
// returns [ 9, 7, 5, 3, 1 ]
s = new Slice( -2, null, -2 );
y = slice( x, s, false );
// returns [ 8, 6, 4, 2, 0 ]
s = new Slice( 2, -2 );
y = slice( x, s, false );
// returns [ 2, 3, 4, 5, 6, 7 ]
s = new Slice( 2, 5 );
y = slice( x, s, false );
// returns [ 2, 3, 4 ]
s = new Slice( 4, 1, -1 );
y = slice( x, s, false );
// returns [ 4, 3, 2 ]
s = new Slice( 5 );
y = slice( x, s, false );
// returns [ 0, 1, 2, 3, 4 ]
s = new Slice( 5, null );
y = slice( x, s, false );
// returns [ 5, 6, 7, 8, 9 ]
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.