slice

Return a shallow copy of a portion of an array.

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 ]
Did you find this page helpful?