@stdlib/array-base-insert-at
v0.1.0
Published
Insert an element into an array.
Downloads
120
Readme
insertAt
Insert an element into an array.
Installation
npm install @stdlib/array-base-insert-atUsage
var insertAt = require( '@stdlib/array-base-insert-at' );insertAt( x, index, value )
Inserts an element into an array.
var x = [ 1, 1, 2, 3, 3 ];
var y = insertAt( x, -3, 4 );
// returns [ 1, 1, 4, 2, 3, 3 ]
var bool = ( x === y );
// returns trueThe function accepts the following arguments:
- x: an input array.
- index: element index.
- value: value to insert.
Notes
- Negative indices are resolved relative to the last array element, with the last element corresponding to
-1. - If provided an out-of-bounds index, the function clamps the index to the beginning or end of the array.
- The function mutates the input array.
Examples
var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
var randi = require( '@stdlib/random-base-discrete-uniform' );
var insertAt = require( '@stdlib/array-base-insert-at' );
// Create an array of random numbers:
var x = discreteUniform( 10, 0, 5, {
'dtype': 'generic'
});
// returns [...]
console.log( x );
// Insert a random element:
var y = insertAt( x, randi( 0, x.length ), randi( 100, 105 ) );
// returns [...]
console.log( y );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-2026. The Stdlib Authors.
