removeAt

Remove an element from an array.

Usage

var removeAt = require( '@stdlib/array/base/remove-at' );

removeAt( x, index )

Removes an element from an array.

var x = [ 1, 1, 2, 3, 3 ];

var y = removeAt( x, -3 );
// returns [ 1, 1, 3, 3 ]

var bool = ( x === y );
// returns true

The function accepts the following arguments:

  • x: an input array.
  • index: element index.

Notes

  • Negative indices are resolved relative to the last array element, with the last element corresponding to -1.
  • If provided out-of-bounds indices, the function returns the input array unchanged; otherwise, the function mutates the input array.

Examples

var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var randi = require( '@stdlib/random/base/discrete-uniform' );
var removeAt = require( '@stdlib/array/base/remove-at' );

// Create an array of random numbers:
var x = discreteUniform( 10, 0, 5, {
    'dtype': 'generic'
});
// returns [...]

console.log( x );

// Remove a random element:
var y = removeAt( x, randi( 0, x.length-1 ) );
// returns [...]

console.log( y );
Did you find this page helpful?