0.2.1 • Published 3 months ago

@stdlib/array-base-at v0.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 months ago

at

NPM version Build Status Coverage Status

Return an element from an array.

Installation

npm install @stdlib/array-base-at

Usage

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

at( x, index )

Return an element from an array.

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

var out = at( x, 0 );
// returns 1

out = at( x, 1 );
// returns 2

out = at( x, -2 );
// returns 3

The function accepts the following arguments:

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

Notes

  • If provided an array-like object having an at method , the function defers execution to that method and assumes that the method has the following signature:

    x.at( index )

    If provided an array-like object without an at method, the function normalizes a provided index and returns a specified element.

  • 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 always returns undefined.

Examples

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

// Define an array:
var x = discreteUniform( 10, -100, 100 );

// Define an array containing random index values:
var indices = discreteUniform( 100, -x.length, x.length-1 );

// Randomly selected values from the input array:
var i;
for ( i = 0; i < indices.length; i++ ) {
    console.log( 'x[%d] = %d', indices[ i ], at( x, indices[ i ] ) );
}

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

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.